A prime number is a numerical value exceeding 1 and divisible only by 1 or itself. In simpler terms, prime numbers are indivisible by any number except for 1 and itself. Illustratively, 2, 3, 5, 7, 11, 13, 17, 19, 23, and so forth, are instances of prime numbers.
Let's examine the C++ code for determining prime numbers. This program in C++ prompts the user to enter a number and then verifies if the input is a prime number.
Example
#include <iostream>
using namespace std;
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
return 0;
}
Output:
Enter the Number to check Prime: 17
Number is Prime.
Enter the Number to check Prime: 57
Number is not Prime.