
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Display Prime Numbers Between Two Intervals in C
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.
Program
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
",i); } return 0; }
Output
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.
Example
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:
",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
",i); } return 0; }
Output
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
Advertisements