C++ Example - Prime Number Check
A prime number, also known as a prime, is infinite in number. A prime number is defined as a natural number greater than 1 that has no other factors besides 1 and itself. Such numbers are called prime numbers.
Example
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout << "Enter a positive integer:";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "is a prime number";
else
cout << "is not a prime number";
return 0;
}
The output of the above program is:
输入一个正整数: 29 是素数other extensions