7. Write a program to check given number is prime or not.

on
#include<iostream>
using namespace std;

int main()
{
    int num,i=2;
    cout<<"Enter any number ::  " ;
    cin>>num;

    while(i<num)
    {
       if(num%i== 0)
       {
           cout<<num<<" is not a prime number. ";
           break;
       }
       i++;
    }
    if(i==num)
        cout<<num<<" is a prime number.";
}

OR
#include<iostream>
using namespace std;

int main()
{
    int num,i=2,flag=1;
    cout<<"Enter any number ::  " ;
    cin>>num;

    while(i<<num/2)
    {
       if(num%i== 0)
       {
           flag=0;
           break;
       }
       i++;
    }
    if(flag)
        cout<<num<<" is a prime number.";
    else
        cout<<num<<" is not a prime number. ";
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s