C++ Program to Print First N Prime Numbers using While Loop
Write C++ Program to Print First N Prime Numbers using While Loop
// CPP Program to Print First N Prime Numbers using While Loop #include <iostream> #include <stdbool.h> using namespace std; bool isPrime(int n) { if(n == 1 || n ==0) return false; int i = 2; while(i < n) { if(n % i == 0) { return false; } i++; } return true; } int main() { int n; cout << "Enter the number :--> "; cin >> n; int i = 1 ; cout << "First " << n << " Prime Numbers are :--> "; int Count = 0; i = 2; while(Count < n) { if(isPrime(i)) { cout << i << " "; Count++; } i++; } return 0; }
Output:
Enter the number :--> 7
First 7 Prime Numbers are :--> 2 3 5 7 11 13 17