C Program to Display Prime Numbers Between Two Numbers

A prime number is the number which can be exactly divided by only 1 and the number itself. For example, 5 can only be exactly divided by 1 and the number itself, so 5 is a prime number. But 8 can be divided by 1, 2, 4 and 8 so it is not a prime number but a composite number. To find and display all prime numbers between 10 and 100, nested loop is to be used. The outer loop runs from 10 to 100 and the inner loop checks whether the number is prime or not.

Source

C program to display all prime numbers between 10 and 100 is shown below:

#include<stdio.h>
int main()
{
    int i, j, c, count=0;
    printf("The prime numbers between 10 and 100 are: ");
    for (i=10; i<=100; i++)
    {
        c = 0;
        j = 1;
        do
        {
            if (i%j ==0)
            {
            c = c+1;
            }
            j = j++;
        } while (j<=i);
        if (c==2)
        {
            printf ("%d ",i);
            count = count+1;
        }
    }
    printf("nThe total number of prime numbers between 100 and 1000 is %d", count);
    return 0;
}

Here, variable i is used for outer loop which runs from 10 to 100, c is the quantity of numbers that exactly divides a number.

NOTE: When c=2, it means that the number is divisible only by 1 and the number itself.

The program can be easily understood from the following steps.

In first loop, inside for loop and outside do loop,

i = 10
c = 0
j = 1

Now inside do loop,

10 % 1 is equal to 0 so c=1
10 % 2 is equal to 0 so c=2
10 % 3 is not equal to 0
.
.
.
10 % 10 is equal to 0 so c=4

After do loop, since c is not equal to 2, so the number is not prime number.

Again, after going to the next for loop, inside for loop and outside do loop,

i = 11,

c=0, (c is initialized to 0 each time a new number is checked because c has to be 2 for each individual number for the number to be prime)

j=1, (j is initialized to 1 each time as the number has to checked from 1,2,3…. till the number itself so that the number can be prime)

Now, inside do loop,

11 % 1 is equal to 0 so c=1
11 % 2 is not equal to 0
.
.
11 % 11 is equal to 0 so c=2

Since c=2, the number is divisible only by 1 and the number itself. So, 11 is a prime number and is displayed on the screen and the value of count is incremented by 1 as it counts the total number of prime numbers.

The loop continues till i=100.

Output

The prime numbers between 10 and 100 are: 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
The total number of prime numbers between 10 and 100 is 21