File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // main.cpp
3
+ // Lecture - 24
4
+ //
5
+ // Created by Prince Kumar on 28/03/19.
6
+ // Copyright © 2019 Prince Kumar. All rights reserved.
7
+ // seive of eratosthenes -- //
8
+
9
+ #include < iostream>
10
+ #include < cstring>
11
+ using namespace std ;
12
+
13
+ void seive ( int n)
14
+ {
15
+ // create boolean array
16
+ bool prime[n+1 ];
17
+ memset (prime, true ,sizeof (prime));
18
+
19
+ for (int p=2 ;p*p<=n;p++)
20
+ {
21
+ // if prime[p] --> false --> it is not prime
22
+ if (prime[p]==true )
23
+ {
24
+ for (int i=p*p;i<=n;i=i+p)
25
+ {
26
+ prime[i]=false ;
27
+ }
28
+ }
29
+ }
30
+
31
+ // print prime numbers
32
+ for (int j=2 ;j<=n;j++)
33
+ {
34
+ if (prime[j]==true )
35
+ cout<<j<<endl;
36
+ }
37
+ }
38
+ int main ()
39
+ {
40
+ cout <<" Enter number upto which you want to print prime numbers " <<endl;
41
+ int n ; cin>>n;
42
+ seive (n);
43
+ }
You can’t perform that action at this time.
0 commit comments