Sunday, March 16, 2014

Prime Number


With the help of this program, you can easily find out whether a given number is prime of not. In this program, we divide the given number from 1 to that number and check the remainder. If it (remainder) becomes zero at any point before reaching that number, the program declares the number as "not prime". Otherwise, if remainder becomes zero at the given number, then the number will be prime. Here 'count' is used as a checker.                                                                                                                                                
#include<iostream.h>

#include<conio.h>

void main ()

{

int n,count=1,rem=1;

cout<<"Enter the number:  ";

 cin>>n;               //entering the number

while (rem!=0)

{

 count++;                //dividing the given the number with each number (from 1 to that num) to chech remainder

 rem=n%count;

}

if (count<n)

 cout<<endl<<"Given number is not prime";

  else if (count==n)         //output

 cout<<endl<<"Given number is prime";

getch ();

}

Widgets

 

Copyright @ 2014 CPP Fuzz.