
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to display the prime numbers in between two intervals
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
- Related Articles
- C++ Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Haskell Program to Display Prime Numbers Between Two Intervals
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- Java Program to Display Prime Numbers Between Intervals Using Function
- Swift program to display prime numbers between intervals using function
- Haskell Program to Display Prime Numbers Between Intervals Using Function
- C++ Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Number Between Two Intervals
- Swift Program to Display Armstrong Number Between Two Intervals
- Golang Program to Display Armstrong Number Between Two Intervals
- Haskell Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Numbers Between Intervals Using Function

Advertisements