Prime number Program in C++
Prime number program in C++ is very simple and easy to write; First of all i will discuss about what is prime number, prime number is number which is only divisible by 1 and itself number. The smallest prime number is 2. When we write code of this program first check number is 2 or not if input number is 2 simply display message on screen Prime number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int number,flag=0;
cout<<"Enter number to check number is prime or not ! ";
cin>>number;
for(int a=1;a<=number;a++)
{
if(number%a==0)
{
flag++;
}
}
if(flag==2)
{
cout<<"Prime number \n";
}
else
{
cout<<"Not a prime number \n";
}
getch();
}
Output:
Enter number to check number is prime or not !
5
Prime number
I hope this code is helpful for your school home work. For more code related to C++ programming please visit our website;
No comments:
Post a Comment