Reputation: 35
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;
}
Upvotes: 0
Views: 100
Reputation: 1944
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
Upvotes: 4