C Program to Print Prime Number Series in a Given Range दी गई Range में Prime Number Series प्रिंट करने का C प्रोग्राम
A prime number is a number greater than 1 that is divisible only by 1 and itself. This program prints all prime numbers between the lower and upper limits entered by the user.
Prime Number वह संख्या होती है जो 1 से बड़ी हो और केवल 1 तथा स्वयं से विभाजित होती हो। यह प्रोग्राम यूजर द्वारा दी गई Lower Limit और Upper Limit के बीच सभी Prime Numbers प्रिंट करता है।
C Program
#include <stdio.h>
int main(void) {
int ll, ul, n, i, isPrime;
int found = 0;
printf("Enter Lower Limit and Upper Limit:\n");
if (scanf("%d%d", &ll, &ul) != 2) {
printf("Invalid input! Please enter two integers.\n");
return 1;
}
if (ll > ul) {
printf("Wrong range! Lower limit must be less than or equal to upper limit.\n");
return 1;
}
printf("Prime numbers between %d and %d are:\n", ll, ul);
for (n = ll; n <= ul; n++) {
if (n < 2)
continue;
isPrime = 1;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", n);
found = 1;
}
}
if (found == 0) {
printf("No prime numbers found in the given range.");
}
printf("\n");
return 0;
}Sample Output 1
Enter Lower Limit and Upper Limit:
10 30
Prime numbers between 10 and 30 are:
11 13 17 19 23 29Sample Output 2
Enter Lower Limit and Upper Limit:
8 10
Prime numbers between 8 and 10 are:
No prime numbers found in the given range.Explanation
The variable found is initialized to 0. When a prime number is found, the program prints it and sets found = 1.
After checking the entire range, if found is still 0, the program prints the message that no prime numbers were found.
found वेरिएबल को शुरुआत में 0 रखा जाता है। जब कोई Prime Number मिलता है, तो प्रोग्राम उसे प्रिंट करता है और found = 1 कर देता है।
यदि पूरी Range चेक करने के बाद भी found का मान 0 रहता है, तो प्रोग्राम बताता है कि दी गई Range में कोई Prime Number नहीं मिला।
Modern C Compatibility Note
This program uses standard C and works with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().
This program prints prime numbers in a given range and displays a message if no prime number is found.
यह प्रोग्राम दी गई Range में Prime Numbers प्रिंट करता है और कोई Prime Number न मिलने पर उचित संदेश दिखाता है।
Comments
Post a Comment