+ 1
What is the simplest algorithm to check whether a no. is prime or not?
how to check a given no. is prime or not? what are the logics? how to write a program for this
2 Antworten
+ 3
#include <iostream>
int main() {
int number;
std::cout << "Enter any desired number: ";
std::cin >> number;
int x = 1, count = 0;
while (true) {
if (number % x == 0) {
count++;
if (count < 3 && x >= number) {
std::cout << number << "Prime number" << std::endl;
break;
}
else if (count >= 3) {
std::cout << number << "Not a Prime number" << std::endl;
break;
}
}
x++;
}
return 0;
}
+ 1
watch my last code