C Programming Code Examples C > Functions Code Examples C program to find prime numbers in given range using functions C program to find prime numbers in given range using functions Write a function in C programming to find prime numbers using function. How to find all prime numbers between two intervals using functions. Display all prime numbers between a given range using function in C programming. Declare function to find all prime numbers in given range First give a meaningful name to our function. Say printPrimes() function will print all prime numbers in given range. Declare one more function say int isPrime(int num); to check prime number. Since we need to print prime numbers in a given range. Hence, we must pass two parameters to function i.e. the upper and lower limit. Pass two integers to the function say printPrimes(int lowerLimit, int upperLimit);. Finally, function will print all prime numbers in given range returning nothing. Therefore, the return type of the function must be void. Function declaration to print all prime numbers in given range - void printPrimes(int lowerLimit, int upperLimit); #include <stdio.h> /* Function declarations */ int isPrime(int num); void printPrimes(int lowerLimit, int upperLimit); int main() { int lowerLimit, upperLimit; printf("Enter the lower and upper limit to list primes: "); scanf("%d%d", &lowerLimit, &upperLimit); // Call function to print all primes between the given range. printPrimes(lowerLimit, upperLimit); return 0; } /* Print all prime numbers between lower limit and upper limit. */ void printPrimes(int lowerLimit, int upperLimit) { printf("All prime number between %d to %d are: ", lowerLimit, upperLimit); while(lowerLimit <= upperLimit) { // Print if current number is prime. if(isPrime(lowerLimit)) { printf("%d, ", lowerLimit); } lowerLimit++; } } /** * Check whether a number is prime or not. * Returns 1 if the number is prime otherwise 0. */ int isPrime(int num) { int j; for(j=2; j<=num/2; j++) { /* * If the number is divisible by any number * other than 1 and self then it is not prime */ if(num % j == 0) { return 0; } } return 1; }