forked from DSC-COEA-Ambajogai/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvedant1707_nprime_6.cpp
59 lines (47 loc) · 1.15 KB
/
vedant1707_nprime_6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Name: Vedant Darji
Program Name : Print first n prime numbers
*/
/*###########################Program Name################################## */
#include <bits/stdc++.h>
using namespace std;
// Function to print first N prime numbers
void print_first_N_primes(int N)
{
// Declare the variables
int i, j, flag;
// Print display message
cout << "\nPrime numbers between 1 and "
<< N << " are:\n";
// Traverse each number from 1 to N
// with the help of for loop
for (i = 1; i <= N; i++) {
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
cout << i << " ";
}
}
// Driver code
int main()
{
int N = 100;
printf(" Enter nos. you want to print");
cin>>N;
print_first_N_primes(N);
return 0;
}
/*###########################Program End################################## */