Important Questions

C++ Program to Check Whether a Number is Prime or Not

//C++ Program to Check Whether a Number is Prime or Not
//header files
#include<iostream.h>
#include<conio.h>

void main()
{

clrscr();
int n, i;
int isPrime = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{

if(n % i == 0)
{

isPrime = 0;
break;

}

}
if (isPrime)

{

cout <<n<< " is a prime number";

}
else
{

cout <<n<< " is not a prime number";

}
getch();

}

Output


You can also see a video how to run this program