nested for loop problem

I cannot get this program to give me primes. It either gives me all the numbers in a range or just the odds.
#include <iostream.h> //for cout and cin
#include <math.h> //for sqrt

int main () //not float main
{
int a, b, c, d; // range and test values

cout << "Enter two integers "; //prompt for integers
cin >> a >> b; //user inputs range

for (c=a; c<=b; c++) //loop to cycle through range
{
for(d=1; d<=sqrt(c); d++) //loop to test for primes
{
if(c%d==0) //checking for even divisibility
break; //continue to next integer

else
cout << " " << c; break; //print prime and continue
}
} //explain what the output indicates
cout << " are the prime numbers in the range " << a << " to " << b << endl;

return 0; //back to os
}
Last edited on
Hi Alogo,

U are Almost done. But the problem lies in the below lines of code.
1
2
3
4
5
if(c%d==0) //checking for even divisibility
break; //continue to next integer

else
cout << " " << c; break; //print prime and continue 


"break" takes the execution out of the loop where as "continue" executes the next state.

Also plz use the "Insert Code" button while posting the code.
It will help the readability of the post.
Last edited on
Topic archived. No new replies allowed.