In this tutorial, you will learn how to display all the Prime Numbers within a Range in C.
Prime Number:
A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.
The program takes the input from the user. It takes the lower range value and upper range value and depending on this it will display all the Prime numbers between those lower and upper-value ranges.
You must know the following first:
C Program to Display all the Prime Numbers in a Given Range.
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 | #include <stdio.h> int main() { int i, j, temp, minRange, maxRange, n; printf("Enter the LowerRange(initial) Value: "); scanf("%d", &minRange); printf("Enter the UpperRange(End) Value: "); scanf("%d", &maxRange); printf("Listing all the Prime Number between %d and %d:\n", minRange, maxRange); for (i = minRange + 1; i < maxRange; i++) { temp = 1; for (j = 2; j < i / 2; j++) if (i % j == 0) { temp = 0; break; } if (temp) printf("%d ", i); } } |
The output of Printing Prime Number in C.
You may go through the following prime program in C.