The Wayback Machine - https://web.archive.org/web/20240721040335/https://www.geeksforgeeks.org/c-program-to-display-prime-numbers-between-two-intervals-using-functions/
Open In App

C Program to Display Prime Numbers Between Two Intervals Using Functions

Last Updated : 08 Jun, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,… are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. 

Prime Numbers between two intervals

 

Example

Input: num1 = 2, num2 = 10

Output: Prime numbers between 2 and 10 are: 2 3 5 7 

Explanation: The prime numbers between the given intervals 2(starting limit) and 10(ending limit) are 2 3 5 and 7

Using For Loop

Below is the C program to display prime numbers between two intervals using functions and for loop:

C




// C Program to demonstrate Prime Numbers
// Between Two Intervals Using for
// loop in a function
#include <stdio.h>
 
// User-defined function to check
// prime number
int checkPrimeNumber(int number)
{
    int i, f = 1;
   
    // Condition for finding the
    // prime numbers between the
    // given intervals
    for (i = 2; i <= number / 2; ++i)
    {
        if (number % i == 0)
        {
            f = 0;
            break;
        }
    }
 
    return f;
}
 
// Driver code
int main()
{
    int num1 = 2, num2 = 10, j, f;
 
    printf("Prime numbers between %d and %d are: ",
            num1, num2);
    for (j = num1; j < num2; ++j)
    {
        // if flag is equal to 1 then
        // it is a prime number
        // calling the function
        f = checkPrimeNumber(j);
 
        if (f == 1)
        {         
            // Printing the result
            printf("%d ", j);
        }
    }
 
    return 0;
}


Output

Prime numbers between 2 and 10 are: 2 3 5 7 

Using While Loop

Below is the C program to display prime numbers between two intervals using functions and while loop:

C




// C Program to demonstrate Prime Numbers
// Between Two Intervals Using for
// loop in a function
#include <stdio.h>
 
int isPrime(int number)
{
    int i;
   
    // condition for finding the
    // prime numbers between the
    // given intervals
    for (i = 2; i <= number / 2; i++) {
 
        // if the number is divisible
        // by 1 and self then it
        // is prime number
        if (number % i == 0) {
            return 0;
        }
    }
 
    return 1;
}
 
// Driver code
int main()
{
    int num1 = 2, num2 = 10;
 
    printf("The prime numbers between %d to %d are: ",
            num1, num2);
 
    while (num1 <= num2)
    {
        // calling the function
        if (isPrime(num1))
        {         
            // printing the result
            printf("%d, ", num1);
        }
 
        num1++;
    }
 
    return 0;
}


Output

The prime numbers between 2 to 10 are: 2, 3, 5, 7, 


Similar Reads

C Program to Display Prime Numbers Between Intervals
Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval. Examples: Input: a = 1, b = 10 Output: 2, 3, 5, 7 Input: a = 10, b = 20 Output: 11, 13, 17, 19Approach 1: In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using for-loop, the numbers
6 min read
C Program to Display Armstrong Number Between Two Intervals
A positive integer with digits a, b, c, d… is called an Armstrong number of order n if the following condition is satisfied: abcd... = a^n + b^n + c^n + d^n +...Example: 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153 Therefore, 153 is an Armstrong number. To display the Armstrong number between two intervals we can use 2 different Methods with 4
3 min read
C Program to Check Whether a Number Can Be Express as Sum of Two Prime Numbers
Prime numbers are numbers that have only 2 factors, 1 and themselves. For example, 2, 3, 5, 7, 11, etc are some of the first prime numbers. Here we will see whether a number can be expressed as the sum of two prime numbers using a C program. Example Input: 7Output: YesExplanation: 7 can be expressed as sum of 2 and 5 which are prime Input: 11Output
2 min read
Program to find Prime Numbers Between given Interval
Given two numbers  a and b as interval range, the task is to find the prime numbers in between this interval. Examples:  Input : a = 1, b = 10Output : 2, 3, 5, 7Input : a = 10, b = 20Output : 11, 13, 17, 19In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using a for-loop, the numbers between
15+ min read
C program to display the animated firecrackers with a message using graphics
In this article, we will discuss how to draw the Rocket using Graphics. Approach: In main() function, call the launch() function. The launch() function is a base which will repeatedly call the fire() &amp; mess() functions until given condition is satisfied.The mess() function will contain the Birthday Message.To portray the birthday message, use t
4 min read
C Program to Print Prime Numbers From 1 to N
Prime numbers are positive integers greater than 1 that have no divisors other than 1 and themselves. In this article, we will learn to generate and display all prime numbers from 1 to N in C programming language.  AlgorithmCheck every number from 1 to N whether it is prime by using isPrime() function.In isPrime() Function,Iterate from 2 to n/2 and
2 min read
C Program to display hostname and IP address
Method 1: There are many ways to find Hostname and IP address of a local machine. Here is a simple method to find hostname and IP address using C program. We will be using the following functions :- gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieve
3 min read
C program to display month by month calendar for a given year
Prerequisite: Find day of the week for a given date Given a year N, the task is to print the calendar for every month of the given year. Implementation: C/C++ Code // C program to print the month by month // calendar for the given year #include &lt;stdio.h&gt; // Function that returns the index of the // day for date DD/MM/YYYY int dayNumber(int da
4 min read
C Program to Display time in Different Country Format
In this article, we will see the time of the different countries. The calculation is based relative to Indian Time. Simple conversion is done between hours, minutes, and seconds. the code includes the 24hr format The below code considers the following countries' examples: Singapore.Newyork.China.Japan.Saudi Arabia. In the below code, all calculatio
6 min read
C Program to Find Armstrong Numbers Between Two Integers
Prerequisite: Program for Armstrong Numbers A positive integer of n digits is called Armstrong number of order n (order is the number of digits) if abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + …. Here, we will build a C Program to print Armstrong Numbers between two integers. Example: 153 is an Armstrong number 1*1*1 + 5*5*5 + 3*3*3 = 153 37
3 min read
three90RightbarBannerImg