C Program To Find Prime Numbers Between Two Intervals, using While Loop


Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested while loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested While Loop: C Program
C Program To Find Prime Number or Not using While Loop
C Program To Find Prime Numbers From 2 To N, using While Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using While Loop


[youtube https://www.youtube.com/watch?v=7JgeqZz9uhw]

YouTube Link: https://www.youtube.com/watch?v=7JgeqZz9uhw [Watch the Video In Full Screen.]

Inner While loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our c program starts checking for divisibility from number 2.

While loop Logic

Outer while loop selects a number for each iteration and stores inside variable start. Inner while loop checks if the selected value(present in variable start) is prime or not. If its a prime number then the variable prime will have value of 1 or else it’ll have value 0 inside it(after completion of inner while loop iteration). If the value present in variable prime is 1, then we print the value present in variable start on to the console window.

Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    int start, end, count, prime, inum;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\n\nPrime Numbers from %d to %d are:\n", start, end);
    while(start <= end)
    {
        inum  = sqrt(start);
        count = 2;
        prime = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                prime = 0;
                break;
            }
            count++;
        }

        if(prime) printf("%d, ", start);
        start++;
    }
    printf("\n\n");

    return 0;
}

Output 1:
Enter start and end value
10
50

Prime Numbers from 10 to 50 are:
11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter start and end value
50
100

Prime Numbers from 50 to 100 are:
53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Output 3:
Enter start and end value
100
200

Prime Numbers from 100 to 200 are:
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,

Output 4:
Enter start and end value
25
50

Prime Numbers from 25 to 50 are:
29, 31, 37, 41, 43, 47,

Output 5:
Enter start and end value
75
150

Prime Numbers from 75 to 150 are:
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Logic To Find Prime Number, using While Loop

In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *