In this tutorial, you will learn how to display prime numbers between two numbers using C program.
Also Read: Check a Number is Prime or Not
C Program to Display Prime Numbers Beeeen Two Numbers
To write a C program to display the prime numbers between two intervals, you can use a function that takes in two integers as arguments and prints out all of the prime numbers within the specified range. Here is an example of how you can implement this function:
Enter the lower bound: 10 Enter the upper bound: 56 The prime numbers between 10 and 56 are: 11 13 17 19 23 29 31 37 41 43 47 53
display_primes() function uses a loop to iterate through the integers from the lower bound to the upper bound, and for each integer it calls the
is_prime() function to determine if it is a prime number. If the number is prime, it is printed out.
is_prime() function uses a loop to iterate through the integers from 2 to the square root of the number, and checks if the number is divisible by any of these integers using the modulus operator (%). If the number is divisible by any of these integers, the function returns false because it is not prime.
If the loop completes without finding a divisor, the function returns true because the number is prime. This process continues until all of the integers in the specified range have been checked, and the prime numbers are printed out.
You can find more C Programming Examples here.