# C Program to display Prime Numbers between Two Intervals

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:

1. Let us assume that a user enters the numbers as 5 and 20.
2. It assigns the value of min=5 and max=20.
3. 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

1. Similarly the outer for loop gets executed till the value of i<max (ie. i<20) and prints the prime numbers.
1. Thus program execution is completed.

## Output:
{"ft_lang_label":"__label__en","ft_lang_prob":0.7227274,"math_prob":0.9971363,"size":4235,"snap":"2019-51-2020-05","text_gpt3_token_len":1396,"char_repetition_ratio":0.22713307,"word_repetition_ratio":0.392,"special_character_ratio":0.3485242,"punctuation_ratio":0.10243902,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99962676,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-18T15:01:22Z\",\"WARC-Record-ID\":\"<urn:uuid:fb28f543-8a08-40a8-877f-df4467aacd6b>\",\"Content-Length\":\"50265\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bba23131-b715-470e-90e5-8f20514e5556>\",\"WARC-Concurrent-To\":\"<urn:uuid:796dbf01-1b36-4dee-a11b-e6e4be5e6c02>\",\"WARC-IP-Address\":\"104.28.22.104\",\"WARC-Target-URI\":\"https://www.codingconnect.net/c-program-prime-numbers-between-two-intervals/\",\"WARC-Payload-Digest\":\"sha1:HFC4GASYM2T4BWAA3SANF2ZFACDJ7ZOS\",\"WARC-Block-Digest\":\"sha1:MXS3ZZKX4YD3QRUE5XGTA4G7J7YLYPDV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250592636.25_warc_CC-MAIN-20200118135205-20200118163205-00210.warc.gz\"}"}