Skip to content

Commit 60df7b0

Browse files
authored
Create Lecture - 24.cpp
1 parent cbc1f8a commit 60df7b0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Lecture - 24.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)