Thos c program to print all the prime numbers between given range Below is the algorithm of this program:
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 30 31 32 33 34 35 36 37 38 | #include <stdio.h> int main() { // Declare the variables. int a, b, i, j, flag; // Ask user to enter lower value of interval. printf("Enter the upper limit: "); scanf("%d", &a); // Ask user to enter upper value of interval. printf("\nEnter the upper limit: "); scanf("%d", &b); // Print the prime numbers. printf("\nPrime numbers between %d and %d are: ", a, b); for (i = a; i <= b; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) printf("%d ", i); } return 0; } |
Program output
1 2 3 4 | Enter the lower limit: 20 Enter the upper limit: 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47 |
Here is same the program using Java programming language.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.