I feel like I'm close to solving this, but I don't know what's wrong. I have to create a program that reads in 5 integers and determine if each is prime, then print them out like it says in the example. Some of the numbers are right, some are wrong. Please help.
On line 22 count is 0. And on line 30 I was trying to say if count ==0 the number must be prime because count increments when the number entered is evenly divisible by a number between 2 and itself. I put "|| num ==2" because when I tried it before without that, program was printing 2 was not prime.
you should make functions, then your code will be much better :)
0, 1 are a pain in the ass, i allways just ask if the number < 2 then it is not a prime.
since it looks like you are just getting into programming I will leave no comment about optimisations :)
#include <iostream>
usingnamespace std;
// i just took your code
// then I put it inside a function
// then I added that number<2 i was speaking of earlier
// i think it works now :)
bool prime(int number)
{
if (number < 2)
returnfalse;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
returnfalse;
}
}
returntrue;
}
int main()
{
int num;
int count=0;
for (int k=1; k<= 5; k++)
{
cin >> num;
cout << num << " ";
if(prime(num))
{
cout << "PRIME ";
}
else
{
cout << "NOTPRIME ";
}
}
cout << endl;
return 0; // muhahaha
}