#include
int main() {
int num, num2,a,b;
do{
cout << "Enter a value in the range of 1 to 100: ";
cin >> num;
if(num < 1 || num > 100)
cout << "Out of range; Please try again... " << endl
<< endl;
}while (num < 1 || num > 100);
int counter = 0;
for (a = 0; a <= num; a++){
}
for (b = 1; b < a; b++){
if (a % b == 0)
counter++;
if (counter == 1)
cout<< "The prime numbers are " << a << " ";
}
return 0;
}
Enter a value from 1 to 100: -5
Out of range; Please try again...
Enter a value from 1 to 100: 200
Out of range; Please try again...
Enter a value from 1 to 100: 101
Out of range; Please try again...
Enter a value from 1 to 100: 45
The prime numbers are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43
This is what the output supposed to look like, but when I ran my code, the prime number output does not come out right. can anyone help me fix the problem?
2007-03-29
19:11:33
·
4 answers
·
asked by
Anonymous
in
Computers & Internet
➔ Programming & Design
I am supposed to ask a user to enter a number from 1 to 100, and loop it until the user enters the value in the right range. Then, once the user enters the right number, I am supposed to find every prime number upto that number the user entered.
2007-03-29
19:13:06 ·
update #1