#include
using namespace std;
typedef unsigned long int ulong;
int main() {
ulong num,j;
do {
system("cls");
do {
cout << "\n\n\tEnter a number greater than one : ";
cin >> num;
} while (num < 2);
for(j=2;j<=num/2;j++)
{
if(num%j==0) {
cout<<"\n\tIt's not a prime, divisible by " << j << "\n\n\n\t";
goto dur; }
}
cout <<"\n\tIt's prime ! \n\n\n\t";
dur: system("pause");
} while (num != 2);
return 0;
}
Hint: instead of num/2 using sqrt(num) is better. (include cmat)
2006-07-23 03:56:23
·
answer #1
·
answered by iyiogrenci 6
·
0⤊
0⤋
let me expalin you wht is a prime number
a prime number is some thing that is divisible by itself and by 1
for example 1, 2, 3, 5 etc.,
keeping this in mind let us try the program
#include
void main()
{
int number ;
cout << "Enter the number :" ;
cin >> number ;
//hope u know above statments i am asking the user to enter the number and storing it in "number" variable.
//ok now v know that if a number is divisible by any other number other than 1 and itself its not a prime number. so let us start our search from 2 to number - 1. so that even if it is divided atleast once its not a prime number.
for(int i = 2; i
{
// to check wether it is divisible r not v use "%"(mod)
if(number % i == 0)
{
//if the result is equal to zero that it means that number is divisible by other number other than 1 and itself.
break ;
//if the condition is true i wil break frm the loop and come out.
}
}
if(i == number)
{
cout << "Entered Number is Prime" ;
}
else
{
cout << "Entered Number is Not Prime" ;
}
//here u need to under stand that when a prime number is entered then it will not enter into if condition even once so it will come out of the loop only when the condition in the loop becomes false ie when i value is equal to the entered number ("number varilable").
}
let us see this with an example :
let us assume that user entered 10
so the for loop starts frm 2 check for the if conditon which is true and breaks
here i = 2 and number = 10
if(i == number) // fails
so "not a prime is displayed"
let us assume that user entered 11
so the for loop starts frm 2 check for the if conditon which is false and continues the loop until i == number and breaks
Note : Here the compiler has not enterd the if case even once.
here i = 11 and number = 11
if(i == number) // true
so "prime is displayed"
Hope that i made u clear with the concept and now u can add a for loop instead of accepting the value frm the user
The main aim should be to under stand the concept then try to make it universal...........
Try to learn concept instead of learing the program
You Can Mail Me If You Have Any Problem In C++ to nicky_kishore@yahoo.co.in
I will help to my level best
Take Care Bye.
2006-07-23 10:41:47
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
/* PROGRAM TO FIND SERIES OF PRIME NUMBERS */
/* prime number r no. which divide by only 1 and itself */
#include
#include
void main()
{
int n,d;
clrscr();
cout<<"Enter any number";
cin>>n;
int prim(int);
p=prim(n);
if(p==0)
cout<<"not a prime number";
else
cout<<"prime number";
getch();
}
int prim(int a)
{
for(int i=2;i
{
int d;
d=a%i;
if(d==0)
return(0);
else
return(1);
}
}
2006-07-23 09:44:27
·
answer #3
·
answered by s_p_freez 2
·
0⤊
0⤋
http://experts.about.com/q/C-1040/Program-determine-prime-numbers.htm
2006-07-23 09:27:24
·
answer #4
·
answered by Frescomoraine 2
·
0⤊
0⤋