In this example, you will learn print maximum prime number between 1 to n (entered by the user). The user will enter 100 input number and program will check all prime numbers till 100 and print highest prime number among them.
A prime is a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11)
1 2 |
Input: 100 Output: 97 (Because 97 is the highest prime between 1 to 100) |
Example: Maximum Number Between 1 to N
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 |
#include<stdio.h> int main() { int num,i; int count = 0,j,maxPrime = 1; printf("Enter a number: "); scanf("%d",&num); for(i = 1; i <= num; i++) { count = 0; for(j = 1; j <= i; j++) { if(i % j == 0) { count++; } } if(count == 2) { if(maxPrime < i) { maxPrime = i; } } } printf("Maximum Prime number between 1 to %d is %d\n",num,maxPrime); //exit status return 0; } |
1 2 |
Enter a number: 100 Maximum Prime number between 1 to 100 is 97 |