I have tried to list prime numbers using C++ Do-While loop but I have trouble and not found problems inside my code. Can any one help me in this?
I have tried so far:
#include <iostream>
using namespace std;
int main()
{
int num2check = 3, innerCheck;
bool isPrime;
do
{
isPrime = true;
innerCheck = 2;
while ((innerCheck < num2check) && (isPrime == true));
{
if (num2check % innerCheck == 0)
{
isPrime = false;
break;
}
else
{
innerCheck++;
}
}
if (isPrime == true)
{
cout << num2check << " is prime.";
num2check++;
}
else
{
num2check++;
}
}while(num2check < 50);
return 0;
}
You need to remove the semicolon in this line
while ((innerCheck < num2check) && (isPrime == true));
In programming standards, the ;
signifies an end of statement, or in this case that it is a null statement, so here the while
loop doesn't executes anything and keeps on running in infinite loop as the condition will always holds true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.