C Program to find Prime Numbers in a given range

In this C Programming example, we will implement the program to find Prime numbers in a given range using the user’s input and print the output on the console. We will implement this program using a For-loop and a function.

1. Prime number

The first question that comes to mind is “what is a prime number?” And the answer is “A Prime Number is a natural number that is only divisible by 1 and itself”.

Example - 2, 3, 5, 7, 11…..

Helpful topics to understand this program better are-


2. C Program to find Prime Numbers using loop

Given two numbers i and j as an interval range, we need to find prime numbers between this interval. Now, Let’s discuss the execution of the program to find prime numbers between two intervals.

Example: i = 10 , j = 20.
Input: i = 10, j = 20
Output: 11, 13, 17, 19
  • The user will be prompted to provide the interval range between the numbers. Then the program will display all the prime numbers between the interval range successively.
  • The values are taken as input and are stored in variables i and j.
  • Then using For-loop, the values between the interval of i and j are traversed iteratively.
  • For each number, in the For-loop we check if the number is prime or not.
  • If the number is found to be prime then the number is printed on the console. Then the next number in the loop is checked, till all numbers are checked and the result is printed in the console.

Let us implement this concept in the c program and find the prime numbers between given range of intervals in C.

#include <stdio.h>

void printPrime(int lowervalue, int uppervalue) {
  int i, j;
  for (i = lowervalue; i < uppervalue; ++i) {
    for (j = 2; j <= i / 2; ++j) {
      if (i % j == 0) {
        printf("%d, ", i);
        break;
      }
    }
  }
}

int main() {
  int lowervalue, uppervalue;
  printf("Enter the lower value and upper value : ");
  scanf("%d %d", &lowervalue, &uppervalue);
  printf("The Prime numbers from %d and %d are:\n", lowervalue, uppervalue);

  printPrime(lowervalue, uppervalue);
  return 0;
}
Output
Enter the lower value and upper value : 2
10
The Prime numbers from 2 and 10 are:
3, 5, 7, 

3. C Program to find Prime Numbers using Functions

Let us now use functions to print prime numbers in a particular range through the user’s input and then print the output in the console.

  • In the below program we have printPrime() the function which will print all the prime numbers in the interval range.
  • We have one more function int isPrime(int number) which is used to check the prime number.
  • Next, we pass two parameters i.e. lowervalue and uppervalue to prompt the user for values and then print the output in the console.
  • Then we pass two integers or values to the function void printPrime(int lowervalue, int uppervalue).
  • Now, the function will print all the prime numbers in the interval range that the user entered and will return nothing as the function is void.

Note: We will just update the method printPrime as the main method will remain the same.

int isPrime(int number) {
  int k;
  for (k = 2; k <= number / 2; k++) {
    if (number % k == 0) {
      return 0;
    }
  }
  return 1;
}

void printPrime(int lowervalue, int uppervalue) {
  while (lowervalue <= uppervalue) {
    // Print if the current number is prime.
    if (isPrime(lowervalue)) {
      printf("%d, ", lowervalue);
    }
    lowervalue++;
  }
}
Output
Enter the lower value and upper value : 5
30
All prime number between 5 to 30 are:
5, 7, 11, 13, 17, 19, 23, 29

4. Conclusion

In this C Programming example, we have discussed how to print all the prime numbers in a given range using the user’s input via two different approaches, using a For-loop and using functions.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index