Program to find all prime numbers between 1 to 30

I have written following code..but it is not working.plzz help

// Program to find all prime numbers between 1 t0 30

#include<iostream>

using namespace std;

int main(){

int i,j=1,c=0,num;

for(i=1;i<30;i++){

while(j<=10){

num=(i%j);

if(num == 0)
c=c+1;
j++;

}

if(c<3){
cout<<i;
}

}

}







Standard input is empty
it looks like you have two separate if statements one in the while loop and one outside of the while loop but inside of the for loop, was that intended?

Also I think what you might want to do is something like the following:
1
2
3
4
5
6
7
8
9
for each number from 1 to 30:
    for each number from 2 to 10:
        check condition and if that is equal to zero: //as you have it  (i%j)
             then store in some structure or print to screen
 //such as an array this depends on what the assignment is asking for 
// i.e. print all prime out all at once or one at a time.
print out all contents in structure if you had a structure or do nothing. 
//depends on what the assignment is asking for 
//i.e. print all prime out all at once or one at a time. 


Please remember to use your code tags.
http://www.cplusplus.com/forum/articles/42672/

Hope this helps,

- Hirokachi
Last edited on
Topic archived. No new replies allowed.