Prime Number Generator
C
Easy
3 views
Problem Description
Print all the prime numbers in the given range. Outer loop iterate numbers, inner loop divisibility check. Optimization: √n
Official Solution
#include <stdio.h>
int main() {
int start, end, i, j, isPrime;
printf("Enter start of range: ");
scanf("%d", &start);
printf("Enter end of range: ");
scanf("%d", &end);
printf("Prime numbers between %d and %d are:n", start, end);
for (i = start; i <= end; i++) {
if (i < 2)
continue;
isPrime = 1;
for (j = 2; j * j <= i; j++) { // check up to sqrt(i)
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d ", i);
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!