C – List All Prime Numbers Between 1 to N
Write a C Program to List All the Prime Numbers Between 1 to N. Where N is the maximum number entered by the user.
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 | #include<stdio.h> int main() { int i, num, maxNumber; int isPrime; printf("Enter max number: "); scanf("%d", &maxNumber); for (int num = 2; num <= maxNumber; num++) { isPrime = 1; for (int i=2; i <= num/2; i++) { if ( num % i == 0) { isPrime = 0; break; } } if ( isPrime == 1 ) printf("%d, ", num); } } |
Compile and run the program.
Enter max number: 50 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,