# Prime number program in c using for loopÂ

Here, you will learn how to write **prime number program in C** to display the prime numbers between two intervals using the for loop.

A **prime number** is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 7 is a prime number because it is only divisible by 1 and 7. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and the number itself.

In the given C program, we have used a for loop to display the prime numbers between two intervals. In this, we have declared a **flag** variable to check whether the number is prime or not with the help of the for loop. At first, we initialized the **flag** as 0.

If **i** is perfectly divisible by **j**, **i** is not a prime number. In this case, the **flag** is set to 1, and the loop is terminated using the **break** statement. When, the **flag** is zero, it prints the prime number, and if the **flag** is one, it exists from the loop. So, if **i** is a prime number after the loop, the **flag** will still be 0, and print the number as prime. However, if **i** is a non-prime number, the **flag** will be 1.

```
#include <stdio.h>
int main(){
int num1,num2,i,j,flag;
printf("Enter the two intervals:");
scanf("%d %d",&num1,&num2);
printf("Prime Numbers between %d and %d:",num1,num2);
for(i=num1+1;i<num2;i++){
flag=0;
//checking number is prime or not
for(j=2;j<=i/2;++j){
if(i%j==0){
flag=1;
break;
}
}
if(flag==0)
printf("%d\n",i);
}
return 0;
}
```

**Output of the above code:**

```
Enter the two intervals:23 55
Prime Numbers between 23 and 55:29
31
37
41
43
47
53
```

```
Enter the two intervals:50 90
Prime Numbers between 50 and 90:53
59
61
67
71
73
79
83
89
```

### Related Articles

**Convert double to int Java**

Average of two numbers in C

Swapping of two numbers in C using pointers

Armstrong number in C using function

Binary to decimal C program

Sum of array elements in C

Random number generator in C

Factorial program in c using while loop

Student mark sheet program in C

C program to sort names in alphabetical order

C program to find largest number in an array

Print first 10 natural numbers using while loop in C

Simple calculator program in C

C program for simple interest

Swap two numbers without using third variable in C

Radix sort program in C

Bit stuffing program in C

Bubble sort program in C

Decimal to hexadecimal in C

Hexadecimal to decimal in C

Quick sort program in C

Average of two numbers in C

Swapping of two numbers in C using pointers

Armstrong number in C using function

Binary to decimal C program

Sum of array elements in C

Random number generator in C

Factorial program in c using while loop

Student mark sheet program in C

C program to sort names in alphabetical order

C program to find largest number in an array

Print first 10 natural numbers using while loop in C

Simple calculator program in C

C program for simple interest

Swap two numbers without using third variable in C

Radix sort program in C

Bit stuffing program in C

Bubble sort program in C

Decimal to hexadecimal in C

Hexadecimal to decimal in C

Quick sort program in C