Write a program in c++ to input a number and check if it is prime or not
Prime Number : A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
#include <iostream>
using namespace std;
int main()
{
int num, prime = 0;
cout << "\t PROGRAM TO CHECK PRIME NUMBER";
cout << "\n Enter A Number : ";
cin >> num;
if (num < 1)
cout << "\n Number should be greater than 0";
else if (num == 1)
cout << "\n 1 is neither Prime nor Composite, It is a Neutral Number";
else
{
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
prime = 1;
cout << " It is not a Prime Number";
break;
}
}
if (prime == 0)
cout << " It is a Prime Number";
return 0;
}
}
OUTPUT
1.
PROGRAM TO CHECK PRIME NUMBER
Enter A Number : 45
It is not a Prime Number
2.
PROGRAM TO CHECK PRIME NUMBER
Enter A Number : 47
It is a Prime Number
0 Comments:
Post a Comment