- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Enter two numbers at console during runtime. Then, declare the flag variable which is used to check whether the number is prime or not with the help of for loop condition.

Whenever, the flag is zero, it prints the prime number and if flag is one, it exists from the loop.

Following is the C program to **display the prime numbers in between two intervals** −

#include <stdio.h> int main(){ int number1,number2,i,j,flag; printf("enter the two intervals:"); scanf("%d %d",&number1,&number2); printf("prime no’s present in between %d and %d:",number1,number2); for(i=number1+1;i<number2;i++){// interval between two numbers flag=0; for(j=2;j<=i/2;++j){ //checking number is prime or not if(i%j==0){ flag=1; break; } } if(flag==0) printf("%d\n",i); } return 0; }

You will see the following output −

enter the two intervals:10 50 the number of prime numbers present in between 10 and 50:11 13 17 19 23 29 31 37 41 43 47

Consider another example, wherein, we are trying to remove the prime numbers in between two numbers.

Following is the C program to **display the numbers in between two intervals excluding prime numbers** −

#include <stdio.h> int main(){ int number1,number2,i,j,flag; printf("enter the two intervals:"); scanf("%d %d",&number1,&number2); printf("the numbers that are present after removing prime numbers in between %d and %d:\n",number1,number2); for(i=number1+1;i<number2;i++){// interval between two numbers flag=1; for(j=2;j<=i/2;++j){ //checking number is prime or not if(i%j==0){ flag=0; break; } } if(flag==0) printf("%d\n",i); } return 0; }

You will see the following output −

enter the two intervals:10 20 the numbers that are present after removing prime numbers in between 10 and 20: 12 14 15 16 18

- Related Questions & Answers
- C++ Program to Display Prime Numbers Between Two Intervals
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C++ Program to Display Armstrong Number Between Two Intervals
- C program to display all prime numbers between 1 to N using for loop
- Program to find Prime Numbers Between given Interval in C++
- Find two distinct prime numbers with given product in C++ Program
- Java program to print the Armstrong numbers between two numbers
- Program to find sum of prime numbers between 1 to n in C++
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range
- Count common prime factors of two numbers in C++
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- C Program to display the numbers in X pattern
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range

Advertisements