hbjhbj

hjh
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (number == count)
{
	break;
}
else if (number % count == 0)
{
	isPrime = false;
	break;
}
else if (count < number)
{
	count++;
}
else // you'll never reach this
{
	isPrime = true;
	break;
}
First, please use code tags. They make reading and commenting much easier.
See http://www.cplusplus.com/articles/jEywvCM9/

Your code in tags, with some whitespace:
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
  char response;
  int number;
  int prime;
  int count = 2;
  int a;
  bool isPrime = false;
  cout << "Please Enter a number Greater than 2: " << endl;
  cin >> number;
  if (number<2) {
    cout << "INVALID ANSWER" << endl;
  }
  else if (number>2) {
    while(count<number) {
      while (true) {
        if (number==count) {
          break;
        }
        else if (number % count == 0) {
          isPrime = false;
          break;
        }
        else if (count<number) {
          count++;
        }
        else {
          isPrime = true;
          break;
        }
      }
      if (isPrime == true)
        cout << "The number " << count<< " is a prime number" << endl;
      else {
        count++;
      }
    }
  }
  cin >> response;
  return 0;
}

Lets say that number is 5.
2!=5
5%2!=0
count++ 2->3
repeat from line 19
3!=5
5%3!=0
count++ 3->4
repeat from line 19
4!=5
5%4!=0
count++ 4->5
repeat from line 19
5==5 break to line 35
isPrime has still the value set on line 11
Line 38: count++ 5->6
repeat from line 18
6 !< 5, continue from line 41
The End.

However, 2, 3 and 5 are all primes, so the program should have printed 3.

The answer, using your code is that you have to change your code more than a bit.

Looking at numbers from 2 to user input is entirely separate from determining whether a number is prime. If you loop count from 2 to number, then you have to check each value of count for primeness and that test may not change count.
im still confused, your saying i should change my code for checking if the number is prime?
I think you should be confused because your code not following the problem. It is somehow trying to find out if "number" is a prime number. But you have to find all prime numbers between 2 and number.
first of all, implement a function which gets an argument an tests if it is a prime number.
like :
bool isPrime(int num);

this function do not cares about your problem. just specifies if num is a prime number or not.
Then you can pass all number between 2 and your variable number to this function. It will say if it is a prime or not. then you can select prime numbers and send them to standard output
Last edited on
Topic archived. No new replies allowed.