I have this code:
#include <iostream>
#include <cmath>
using namespace std;
int n, liczba;
int main()
{
cin >> n;
for (int i = 0; i < n; i++) {
cin >> liczba;
if (liczba < 2) {
cout << "NIE" << endl;
} else if (liczba == 2) {
cout << "TAK" << endl;
}
for (int i = 2; i < liczba; i++) {
if (liczba % i == 0) {
cout << "NIE" << endl;
break;
} else if (liczba % i != 0) {
cout << "TAK" << endl;
break;
}
}
}
return 0;
}
This program is supposed to write yes "TAK"
or no "NIE"
whether the number you input is prime or isn't. Variable n
is the number of numbers you want to input into program, and liczba
is the number you want to check if it's prime or not. It seems to work fine expect one significant thing. If I input number 9 it says yes "TAK"
instead of no "NIE"
.. I discovered that this happens to numbers: 9,27,45,63,81
and so on.. if I add 18
starting from 9
it will happen every time.
What's wrong with my code?