NOTE : YOU CAN DOWNLOAD EXE FILE HERE: SIEVE.EXE
SORUCE CODE (RTF): source.cpp click here
1 #include <iostream>
2 #include <conio.h>
3 using namespace std;
4 int main()
5
6 {
7
8 int number,n=2;
9 cout<<"Program to find out Prime number by sieve method\n\n\a";
10
11 cout<<"Enter upto which number you want to find out the prime number\n";
12 cin>>number;
13 int arr[number];
14 for(int i=0;i<number;i++)
15 arr[i]=i+1;
16 cout<<" Original number:\n";
17
18 for(int i=0;i<number;i++)
19 {
20
21
22 cout<<arr[i]<<"\t\a";
23
24 }
25 while((n*n)<number) // while n2 is less than original number
26
27 {
28
29 for(int i=n;i<number;i++)
30 {
31 if(arr[i]%n==0)
32 {
33 arr[i]=-1;
34 }
35 }
36 n++;
37
38
39 }
40 cout<<" \n\n Sieve Format:\n";
41
42 for(int i=0;i<number;i++)
43 {
44
45 if(arr[i]==-1) cout<<"X\t";
46 else
47
48 cout<<arr[i]<<"\t\a";
49
50 }
51 cout<<" Prime number's are given belows:\n";
52
53 for(int i=0;i<number;i++)
54 {
55
56 if(arr[i]==-1) continue;
57
58
59 cout<<arr[i]<<"\t\a";
60
61 }
62
63 getch();
64 }
NOTE : YOU CAN DOWNLOAD EXE FILE HERE: SIEVE.EXE
Comments
Post a Comment
share your thoughts ....