Explore a little.C ProgrammingC++HTMLVisual BasicSystem AnalysisE-CommerceBCA Notes
LEARN SOMETHING NEW

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

Read4BCA learning libraryFree access

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

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

You can also see a video how to run this program

This lesson preserves the original Read4BCA material. Some programming examples use older tools and syntax. View original page.