Prime Checker Library
C
Easy
4 views
Problem Description
Create a function isPrime() to check the number. Create another function printPrimesInRange() to print all the primes in the range using isPrime(). Example of modular code.
Official Solution
#include <stdio.h>
/* Function to check if a number is prime */
int isPrime(int num) {
int i;
if (num <= 1)
return 0;
for (i = 2; i * i <= num; i++) {
if (num % i == 0)
return 0;
}
return 1;
}
/* Function to print primes in a given range */
void printPrimesInRange(int start, int end) {
int i;
printf("Prime numbers between %d and %d:n", start, end);
for (i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("n");
}
int main() {
int start, end;
printf("Enter start of range: ");
scanf("%d", &start);
printf("Enter end of range: ");
scanf("%d", &end);
printPrimesInRange(start, end);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!