• ## Discover

1. Home >
2. All Categories >
3. Computers & Internet >
4. Programming & Design >
5. Resolved Question
Member since:
November 07, 2009
Total points:
178 (Level 1)

## Resolved Question

Show me another »

# Write a c++ program to check whether a given number is prime or not?

by 2441139
Member since:
March 25, 2008
Total points:
1,137 (Level 3)

## Best Answer - Chosen by Voters

Hi,try this one...

#include<iostream.h>
int main()
{
int num;
cout << "Enter a number ";
cin >> num;
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
cout << "\n" << num << " is not a prime number.";
break;
}
i++;
}
if(i==num)
cout << "\n" << num << " is a prime number.";
return 0;
}
100% 1 Vote
• 2 people rated this as good

There are currently no comments for this question.

• Member since:
September 13, 2010
Total points:
495 (Level 2)
The program for prime number is as follow.........

#include<iostream.h>
#include<conio.h>

main()
{
int a,c=0,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a=0)
{
c=c+1;
}
}
if (c=2)
{ printf("the given number is prime"); }
else
printf("the given number is not prime");

getch();
}
• by Irum
Member since:
September 23, 2013
Total points:
104 (Level 1)
//Prime Numbers generation in C++
//Using for loops and conditional structures
#include <iostream>
using namespace std;

int main()
{
int a = 2; //start from 2
long long int b = 1000; //ends at 1000

for (int i = a; i <= b; i++)
{

for (int j = 2; j <= i; j++)
{
if (!(i%j)&&(i!=j)) //Condition for not prime
{
break;
}

if (j==i) //condition for Prime Numbers
{
cout << i << endl;

}
}
}
}
- See more at: http://www.programmingtunes.com/generation-of-prime-numbers-c/#sthash.YoWHqYcm.dpuf
• Member since:
September 24, 2012
Total points:
121 (Level 1)
#include <iostream.h>
#include <conio.h>
int main ()
{
int i=2,N;
cout<<"Enter any number"<<endl; cin>>N;
if (i==N)
cout<<"Number is prime";
else
{
while (N>i)
}
if (N%i==0)
break;
i++;
if (i==N)
cout<<"Number is prime";
else
cout<<"Number is not prime";
getch();
return 0;
}

### Source(s):

OWN KNOWLEDGE
• Member since:
October 30, 2013
Total points:
100 (Level 1)
write a program to verify that mean of the means of smple size 3, taken from a finite population 1,3,5,7 and 9 is equal to population mean..........???
• by HI
Member since:
August 07, 2009
Total points:
157 (Level 1)
veryfy balagurusawmy book...............