CPP04 – (b) Write a CPP program to print whether a number is prime or not .

By | October 17, 2018

(b) Write a CPP program to print whether a number is prime or not .

CODE:-

#include<iostream.h>
#include<conio.h>
void main()
{
int NUM,I;
clrscr();
cout<<“ENTER A NUMBER :”;
cin>>NUM;
for(I=2;I<NUM;I++)
{
if(NUM%I==0)
break;
}
if(I==NUM)
cout<<NUM<<” IS A PRIME NUMBER”<<endl;
else
cout<<NUM<<” IS NOT A PRIME NUMBER”<<endl;
getch();
}

#include<iostream.h>
#include<conio.h>
void main()
{
  int NUM,I;
  clrscr();
  cout<<"ENTER A NUMBER :";
  cin>>NUM;
  for(I=2;I<NUM;I++)
  {
    if(NUM%I==0)
       break;
  }
  if(I==NUM)
     cout<<NUM<<" IS A PRIME NUMBER"<<endl;
  else
     cout<<NUM<<" IS NOT A PRIME NUMBER"<<endl;
getch();
}

SCREEN SHOTS:-

OUTPUT:-