C program to find prime numbers between given interval using functions

Write a function in C programming to find all prime numbers between two intervals. How to find all prime numbers between two intervals using the concept of functions. Display all prime numbers between a given range using functions in C programming.

Example

Input

Input lower limit: 10
Input upper limit: 50

Output

Prime numbers between 10-50 are: 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Required knowledge

Basic C programming, Function

Must know -

Declare function to find all prime numbers in given range

  1. First give a meaningful name to the function. Say printPrimes() function we will define to print all prime numbers in given range. Declare one more function say int isPrime(int num); to check whether a number is prime.
  2. 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);.
  3. Finally, the function 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);

Program to find all prime numbers in given range using function

/**
 * C program to list all prime number between an interval using function.
 */

#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)
    {
        // Check whether the current number is prime or not.
        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 i;  
      
    for(i=2; i<=num/2; i++)    
    {    
        /*   
         * If the number is divisible by any number   
         * other than 1 and self then it is not prime  
         */    
        if(num % i == 0)    
        {  
            return 0;  
        }    
    }   
      
    return 1;   
}

Output

Enter the lower and upper limit to list primes: 10 50
All prime number between 10 to 50 are: 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>