C Program To Find Prime Numbers Between Range, using For 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 for 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 For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop
C Program To Find Prime Numbers From 2 To N, using For Loop

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


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

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

Logic To Find Prime Number Between Range, using For Loop

We ask the user to enter start and end value. We check if the value of variable start is greater than variable end. If true, we swap the values of variable start and end.

Outer For Loop Logic

We assign value of start to num and keep iterating the for loop until num is less than or equal to value of variable end. For each iteration of outer for loop num will increment by 1, from start to end value.

Inner For loop Logic

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

The selected number(selected by outer for loop and stored in variable num), is divided by numbers 2 to num-1 times. If num is perfectly divisible by any number between 2 to num-1, then the number is not a prime number, else its a prime number.

Source Code: C Program To Find Prime Numbers Between Range, using For Loop

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

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

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

    if(start > end)
    {
        temp = start;
        start= end;
        end  = temp;
    }

    printf("Prime Numbers between %d and %d are\n", start, end);

    for(num = start; num <= end; num++)
    {
        prime = 1;
        inum  = sqrt(num);
        for(count = 2; count <= inum; count++)
        {
            if(num % count == 0)
            {
                prime = 0;
                break;
            }
        }

        if(prime) printf("%d,\t", num);
    }

    return 0;
}

Output 1:
Enter start and end value
10
20
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 2:
Enter start and end value
20
10
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 3:
Enter start and end value
25
60
Prime Numbers between 25 and 60 are
29, 31, 37, 41, 43, 47, 53, 59,

Output 4:
Enter start and end value
50
150
Prime Numbers between 50 and 150 are
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Output 5:
Enter start and end value
5
41
Prime Numbers between 5 and 41 are
5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,

Logic To Find Prime Number, using For Loop

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

For example,
If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

User entered number 41 is not perfectly divisible by any number between 2 to 6, so number 41 is 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.

You can also watch C Program To Find Prime Numbers Between Two Intervals, using While Loop video tutorial.

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 *