C++ Program to Print First n Prime Numbers
Here you will get the source code of C++ program to print first n prime numbers.
Example Code to Print First n Prime Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; int main() { int i, j, num, count=0; cout<<"How many prime numbers you want : "; cin>>num; for (i = 2; count < num; i++) { for (j = 2; j <= i; j++) { if (i % j == 0) break; } if (i == j) { cout << i << " "; count++; } } return 0; } |
Output
How many prime numbers you want : 15
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Check out our other C++ programming Examples