int main() { setlocale(LC_ALL, "Russian"); int N, y, i; bool Prime; cout << "Введите число: "; cin >> N; do { Prime = true; y = N % 10; for (i = 2; i <= (sqrt(abs(y))); i++) { if (y % i == 0) { Prime = false; break; } } if ((Prime) & (y != 0) & (y != 1)) cout << y << " - простое" << endl; else cout << y << " - не простое" << endl; N = N / 10; } while (N != 0); system("pause"); return 0; }
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "Russian");
int N, y, i;
bool Prime;
cout << "Введите число: "; cin >> N;
do {
Prime = true;
y = N % 10;
for (i = 2; i <= (sqrt(abs(y))); i++) {
if (y % i == 0) {
Prime = false;
break;
}
}
if ((Prime) & (y != 0) & (y != 1))
cout << y << " - простое" << endl;
else
cout << y << " - не простое" << endl;
N = N / 10;
} while (N != 0);
system("pause");
return 0;
}
Так как язык не указан, приведу пример на SWI-Prolog.
Код:
read_int(Int) :- read(Int), integer(Int).split_int_by_numbers(0, []) :- !.split_int_by_numbers(N, [Number|Ints]) :- Number is mod(N, 10), RestN is div(N, 10), split_int_by_numbers(RestN, Ints).test_to_div(_, []).test_to_div(N, [Number|Ints]) :- mod(N, Number) =:= 0, test_to_div(N, Ints). test(Int) :- split_int_by_numbers(Int, Numbers), test_to_div(Int, Numbers), write(Int), write(" - Yes!"), nl.test(Int) :- write(Int), write(" - No!"), nl.?- read_int(Int), test(Int).