Signup/Sign In

Program for Checking Prime Number in C++

Following is the program to check whether a number is prime or not.

#include<iostream.h>

void main()
{
    int no, i;
    cout<<"Enter a number to be checked (greater than 2) : ";
    cin>>no;
    for(i=2;i<no;i++)
    {
        if(no%i==0)
        {
            break;
        }
    }
    if(no==i)
    {
        cout<<"\n"<<no<<" is a prime number";
    }
    else
    {
        cout<<"\n"<<no<<" is not a prime number";
    }
}

Enter a number to be checked (greater than 2) : 5 5 is a prime number