Program to check a number is prime

A Prime Number is a number which can be divided only by 1 or by itself. And it must be a whole number greater than 1. No even number, except 2, is prime number as 2 is divided by 1 or 2 only i.e by 1 or by itself. Following program check a number is prime or not. The approach is simple, we ask user to enter a number (num). Then a loop is initiated to num-1 and we apply modulus operator to determine whether it gives remainder or not. If it gives remainder 0 , it is not a prime number.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

int main()

{

        clrscr();

        int num;

        int i=2;

        cout<<” Enter a Number which you want to determine”;

        cin>>num;

        while(i<=num-1)

        {

               if(num%i==0)

               {

                       cout<<“\n\n\t”<<num<<” is not a prime number”;

                       break;

               }

               i++;

        }

        if(i==num)

        {

               cout<<“\n\n\t”<<num<<” is prime number”;

        }

        getch();

}

Mohit Arora
Follow me