#include <iostream>
usingnamespace std;
int main()
{
int n,flag,number;
char c;
while(true)
{
cout << "Enter an integer greater than 1 to check if it is a prime number: ";
cin>>n;
flag=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
number = n;
if(flag==0)
{ cout << "The number " << number << " is a prime number." << endl;}
else
{cout << "The number " << number << " is NOT a prime number." << endl;}
cout << "Do you want to enter another number? (y or n): ";
cin>>c;
if(c=='y')
{
continue;
}
else
{
break;
}
cout << "Exiting program ..." << endl;
}
return 0;
}
Your code is fairly hard to read because of the use of inconsistent indentation. I suggest you try to improve that aspect of the program to make your program easier to read.
Now to the problem. I would recommend changing your while() loop condition to
while(c == 'y' || c == 'Y')
And then you won't need the continue, break, and the if() statement at the end of your loop. Be sure to initialize c to some value before the loop. And then move your "Exiting program ..." statement to after the while() loop.
Your program is very inconsistently indented and, hence, incredibly difficult to read.
I think the break in line 34 would exit the while loop whose opening brace is line 8 and closing brace is line 37 (but it's very hard to see!). The program then ends.
Perhaps you want to put your "Exiting program ..." output BEFORE the break statement on line 34. Otherwise it will never run.
#include <iostream>
usingnamespace std;
int main()
{
int n, flag, number;
char c;
while(true)
{
cout << "Enter an integer greater than 1 to check if it is a prime number: ";
cin >> n;
flag = 0;
for(int i = 2; i * i <= n; i++)
{
if(n % i == 0)
{
flag = 1;
break;
}
}
number = n;
if(flag == 0)
{
cout << "The number " << number << " is a prime number." << endl;
}
else
{
cout << "The number " << number << " is NOT a prime number." << endl;
}
cout << "Do you want to enter another number? (y or n): ";
cin >> c;
if(c == 'y')
{
continue;
}
else
{
break;
}
cout << "Exiting program ..." << endl;
}
return 0;
}
Do you see how much easier the above is to read and follow the program logic?
Also you should start using meaningful variable names. Your use of the single letter variable names is not a good practice and as your programs grow their use will tend to make your programs unreadable as well.
#include <iostream>
usingnamespace std;
int main()
{
int n, flag, number;
char c;
while(true)
{
cout << "Enter an integer greater than 1 to check if it is a prime number: ";
cin >> n;
flag=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
number = n;
if(flag==0)
{
cout << "The number " << number << " is a prime number." << endl;}
else
{
cout << "The number " << number << " is NOT a prime number." << endl;
}
cout << "Do you want to enter another number? (y or n): ";
cin >> c;
if( c == 'y' )
{
continue;
}
else
{
cout << "Exiting program ..." << endl;
break;
}
}
return 0;
}
I improved the indentation inconsistencies. And I replace the cout "Exiting program ..." before break and it solved this problem.