-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject_work_11.CPP
More file actions
executable file
·45 lines (37 loc) · 1.05 KB
/
Copy pathproject_work_11.CPP
File metadata and controls
executable file
·45 lines (37 loc) · 1.05 KB
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
//print n prime nos
#include <iostream> // library for basic input/output functions
#include <math.h> // library for using math functions
using namespace std;
int main()
{
int n, status = 1, num = 3, count, c;
//prompting user to enter number of prime numbers to print
cout << "Enter the number of prime numbers to print\n";
cin >> n;
// input is valid display first prime number
if ( n >= 1 )
{
cout << "First " << n <<" prime numbers are :-" << endl;
cout << 2 << endl;
}
for ( count = 2 ; count <=n ; ) //loop that will iterate through n numbers
{
for ( c = 2 ; c <= (int)sqrt(num) ; c++ )// for each element check whether it is prime or not
{
//if number is completely divisible by a number other than 1 and itselft,then number is not prime
if ( num%c == 0 )
{
status = 0;
break;
}
}
//if it is a prime number, print it
if ( status != 0 )
{
cout << num << endl;
count++;
}
status = 1;
num++;
}
}