My code is supposed to check from starting at number 2 if it's a prime number and which others that are prime numbers up to 100. If it is a prime number the program should print "Prime" and if it's not a prime number the program should print "Num".
I've got one for loop that checks if the number is a prime number. It works fine for the first one which is 2. But all the numbers after 2 is according to my program a number and not a prime number.
I got an int variable called "check" which has the value of "n".
At the end of the outer for-loop I increase "n" (n++) and since I'm initializing the "check" variabel in the beginning of this for-loop shouldn't the n-value change there too?
Is it the "check" variable in the inner for-loop that doesn't change it's value?
Am I even on the right track to solving the problem or should I try antoher solution? Or is my solution possible to make correct?
Code:
#include <iostream>
using namespace std;
int main() {
bool is_prime = true;
int n = 2;
int range = 100;
// Kolla hur många tal som är primtal 2-100
for (int j = 0; j < range; j++) {
int check = n;
// Checks if a number is a prime
for (int i = 2; i < check; i++) {
if (n % i != 0) {
}
else {
is_prime = false;
}
}
if (is_prime && n > 1) {
cout << "Prime: " << n << endl;
}
else {
cout << "Num: " << n << endl;
}
n++;
}
return 0;
}