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.