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.

**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, If else, While loop, Functions

## 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);

## 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)
{
// 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 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 😉

### Recommended posts

- Function and recursion programming exercise index.
- C program to find strong numbers in given range using function.
- C program to find armstrong numbers in given range using function.
- C program to find perfect numbers in given range using function.
- C program to check even number using function.
- C program to find power using recursion.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>