I'm trying to print out whether a number is prime or not up to and including the user inputted integer. The problem I'm having is that regardless of the user inputted integer, the loop only runs up to 4. How do I break from the nested loop without ending the opening loop?
The code I'm using is below.
Many thanks.
#include <iostream>
using namespace std;
int main ()
{
int N, a;
loop:
cout << "Please enter a positive integer or 0 to quit: ";
cin >> N;
if (N==0) return 0;
else {
while (N<1)
{
cout << "Please enter a positive integer or 0 to quit: ";
cin >> N;
if (N==0) return 0;
}
for (int j=1;j<=N;j++)
{
if (j==1||j==2) {cout << j << " is prime." << endl; cout << "\n";}
else
{
for (int i=2;i<j;i++)
{
if (j%i==0)
{
cout << j << " is not prime.\nIt is divisible by " << i << "." << endl;
cout << "\n";
j=a;
break;
}
}
if (j!=a) {cout << j << " is prime." << endl; cout << "\n";}
}
}
goto loop;
}
return 0;
}