To write a program to display Prime Numbers between Two Intervals we should know how to find a Number is Prime Number or Not?

To find a Number is Prime Number or Not, you can check out the following link:**C program to find a Number is Prime Number or Not**

## Program code to display Prime Numbers between Two Intervals:

/* Program to display Prime numbers between two Intervals */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int n, i, j, min, max, flag=0; clrscr(); printf("n Enter two numbers(Intervals) : "); scanf("%d%d",&min,&max); printf("n The prime numbers between %d and %d are :",min,max); for(i=min+1;i<max;i++) { flag=0; for(j=2;j<=sqrt(i);j++) //Loop to check whether 'i' (number) is divisible by any number between 2 and sqrt(i) { if(i%j==0) { flag=1; break; } } /* if condition to check and print if the Number is Prime Number */ if (flag==0) { printf(" %d ",i); } } getch(); }

## Working:

- First the computer reads two numbers (Intervals) from the user.
- Then using outer for loop the numbers between the intervals are take one by one.
- Then using inner for loop it checks whether the number is prime number or not.
- Then if condition is used to check and print if the number is prime number.
- Thus all the prime numbers are printed.

## Step by Step working of the above Program Code:

- Let us assume that a user enters the numbers as 5 and 20.
- It assigns the value of min=5 and max=20.
- It assigns the value of i=min+1 (ie.
**i=6**) and the loop continues till the condition of the outer for loop is true.

**3.1.** i<max (6<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.1.1.** j<=sqrt(i) (2<=√6) inner for loop condition is true

i%j==0 (6%2==0) if condition is true

It assigns flag=1

breaks the loop and comes out of the inner for loop

flag==0 (1==0) if condition is false

So it goes for next iteration of outer for loop.

i++ (i=i+1) So **i=7**

**3.2.** i<max (7<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.2.1.** j<=sqrt(i) (2<=√7) inner for loop condition is true

i%j==0 (7%2==0) if condition is false

j++ (j=j+1) So j=3

**3.2.2.** j<=sqrt(i) (3<=√7) inner for loop condition is false

It comes out of the inner for loop.

flag==0 (0==0) if condition is false

So it **prints 7** and goes for next iteration of outer for loop.

i++ (i=i+1) So **i=8**

**3.3.** i<max (8<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.3.1.** j<=sqrt(i) (2<=√8) inner for loop condition is true

i%j==0 (8%2==0) if condition is true

It assigns flag=1

breaks the loop and comes out of the inner for loop

flag==0 (1==0) if condition is false

So it goes for next iteration of outer for loop.

i++ (i=i+1) So **i=9**

**3.4.** i<max (9<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.4.1.** j<=sqrt(i) (2<=√9) inner for loop condition is true

i%j==0 (9%2==0) if condition is false

j++ (j=j+1) So j=3

**3.4.2.** j<=sqrt(i) (3<=√9) inner for loop condition is true

i%j==0 (9%3==0) if condition is true

It assigns flag=1

breaks the loop and comes out of the inner for loop

flag==0 (1==0) if condition is false

So it goes for next iteration of outer for loop.

i++ (i=i+1) So **i=10**

**3.5.** i<max (10<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.5.1.** j<=sqrt(i) (2<=√10) inner for loop condition is true

i%j==0 (10%2==0) if condition is true

It assigns flag=1

breaks the loop and comes out of the inner for loop

flag==0 (1==0) if condition is false

So it goes for next iteration of outer for loop.

i++ (i=i+1) So **i=11**

**3.6.** i<max (11<20) outer for loop condition is true

It assigns flag=0 , j=2

**3.6.1.** j<=sqrt(i) (2<=√11) inner for loop condition is true

i%j==0 (11%2==0) if condition is false

j++ (j=j+1) So j=3

**3.6.2.** j<=sqrt(i) (3<=√11) inner for loop condition is true

i%j==0 (11%3==0) if condition is false

j++ (j=j+1) So j=4

**3.6.3.** j<=sqrt(i) (4<=√11) inner for loop condition is false

It comes out of the inner for loop.

flag==0 (0==0) if condition is false

So it **prints 11** and goes for next iteration of outer for loop.

i++ (i=i+1) So **i=12**

- Similarly the outer for loop gets executed till the value of
**i<max**(ie.**i<20**) and prints the prime numbers.

- Thus program execution is completed.