C Program to Print First n Prime Numbers using For Loop
Write a Program to Print First n Prime Numbers using While Loop
// C Program to Print First n Prime Numbers using While Loop #include <stdio.h> #include <stdbool.h> bool isPrime(int n) { if(n == 1 || n ==0) return false; int i; for(i = 2; i < n; i++) { if(n % i == 0) { return false; } } return true; } int main() { int n; printf("Enter the number :--> "); scanf("%d", &n); int i = 1 ; printf("First %d Prime Numbers are :--> ", n); int count = 0; i = 2; for(; count < n; ) { if(isPrime(i)) { printf("%d ",i); count++; } i++; } return 0; }
Output:
Enter the number :--> 7
First 7 Prime Numbers are :--> 2 3 5 7 11 13 17