+ 3

How can i write a program in c++ to execute prime numbers

17th Aug 2017, 3:27 PM
harshal solanki
harshal solanki - avatar
3 odpowiedzi
+ 3
#include<iostream.h> #include<conio.h> int main() { int n,i,c; cout<<""Enter Number"<<endl;; cin>>n; for(i=1;i<=n;i++) { if(n%i==0) { c++; } } if(c==2) { cout<<n<<"is a prime number"; } else cout<<n<<"is a composite number; } Please leave a like i typed the whole program and its very inconvinient to type in the answer dialog box.
17th Aug 2017, 4:00 PM
King Infinity
King Infinity - avatar
+ 1
You need a loop to check if a number is divisible by any number smaller than it. You should come up with your own code and take this as a challenge, but if you want a little help here is my version in c++ https://code.sololearn.com/cPWk2Rg5q1de You can also optimize the code by only checking for divisors up to the square root of the number, but that would be harder and unnecessary for small numbers
17th Aug 2017, 4:01 PM
prenone
+ 1
thnk u
17th Aug 2017, 4:04 PM
harshal solanki
harshal solanki - avatar