Determining if numbers are prime

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// ------------------------------------------------------------------
// Function: Read five (5) positive whole numbers, and determine if
//           each is prime.
//
// Examples: input:  7  8 11 15  2
//           output: 7 PRIME 8 NOTPRIME 11 PRIME 15 NOTPRIME 2 PRIME
//
// NOTE: A nested loop is required.
//
// ------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   int num;
   int count=0;

   for (int k=1; k<= 5; k++)
   {
     cin >> num;
     cout << num << " ";

     for(int i=2;i<num;i++)
     {
      if(num%i==0)
      {
        count++;
      }
     }
      if(count==0||num==2)
      {
        cout << "PRIME ";
      }
      else
      {
        cout << "NOTPRIME ";
    }
   }
   
    
     
   cout << endl; return 0; // DO NOT MOVE, MODIFY or DELETE.
}
Last edited on
What is the value of "count" on line 22?
Why "0 or 2" on line 30?
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.
Is the count 0 on line 22 on every iteration? What if the previous number did increment the count?

If num==2, then the loop on lines 23--29 is not iterated at all and thus the count remains 0 (if it is 0 at line 22).
Ok, so I added count = 0; on line 22.

Now the program says 1 is prime. I tried to change line 25 to if(num%i==0 && num!=1) but that didn't work.
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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace 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)
    return false;

  for(int i=2;i<num;i++)
  {
    if(num%i==0)
    {
        return false;
    }
  }
  return true;
} 

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
}
Last edited on
Never mind, I fixed it. Thanks a lot for your help!

Edit: @Gamer2015 thanks too but I got it to work
Last edited on
good to know, no problem x'D
Topic archived. No new replies allowed.