Program to detect given number is prime or not.

Check Prime or Not in C++

To check whether the number is a prime number or not a prime number in C++ programming, you have to ask to the user to enter a number and start checking for prime number. If number is divisible by 2 to one less than that number (n-1), then the number is not prime number, otherwise it will be a prime number.

/* C++ Program - Check Prime or Not */
  
#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int num,i,count=0;
 cout<<"Enter a number:";
 cin>>num;
 for(i=2;i<num;i++)
 {
  if(num%i==0)
  {
   count++;
   break;
  }
 }
 if(count==0)
 {
  cout<<"This is a prime number";
 }
 else
 {
  cout<<"This is not a prime number";
 }
 getch();
}

Outputs:-


Post a Comment

0 Comments