HELP code

with this new code I rewrote to determine if a number is prime or not, the code works. My problem is that it only runs the program once, and I want it to keep going. Any help would be appreciated on where to fix the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using std:cin;
using std:cout;
using std:endl;
 #include <cmath>

int main(){
unsigned long n;
while (cin>>n){
for (int I=1; I<=n; I++)
{if (n==0)
{cout<<"0"<<endl;}
else if (n%2==0 && n!=2)
{cout<<"0"<<endl;}
else if (n%3==0)
{cout<<"0"<<endl;}
else if (n%5==0)
{cout<<"0"<<endl;}
else if (n%7==0)
{cout<<"0"<<endl;}
else {
cout<<"1"<<endl;}
return 0;
}}}
It would help you in the long term, and everyone else, if you used proper indentation in your code.

Here's your code with proper indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using std:cin;
using std:cout;
using std:endl;
#include <cmath>

int main() {
    
    unsigned long n;
    
    while (cin>>n) {
        for (int i=1; i<=n; i++)
        {
            if (n==0)
            {
                cout<<"0"<<endl;
            }
            else if (n%2==0 && n!=2)
            {
                cout<<"0"<<endl;
            }
            else if (n%3==0)
            {
                cout<<"0"<<endl;
            }
            else if (n%5==0)
            {
                cout<<"0"<<endl;
            }
            else if (n%7==0)
            {
                cout<<"0"<<endl;
            }
            else
            {
                cout<<"1"<<endl;
            }
            return 0;
        }
    }
}


You can now see that you have a return 0; within your inner for loop. This immediately ends your program. There's other issues as well, but removing the return statement is a good start.
Topic archived. No new replies allowed.