Table of Contents
Code to Display all prime numbers in an intervals of C
In this article, we will discuss the concept of Code to Display all prime numbers in an intervals of C
In this program, we are going to learn how to write the code to display prime numbers between two intervals using different methods in C language.
This is done using for loop , while loop , do-while loop and function in C language
Display prime numbers between two intervals
Display prime numbers between two intervals using for loops
In this program, we will print prime numbers between two intervals using a for loop in C language
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,min,max;
printf("Enter two number with intervals\n");
scanf("%d %d",&min,&max);
printf("Prime numbers %d to %d are\n",min,max);
for(i=min; i<=max; i++){
int count=0;
for(j=1; j<=i; j++){
if(i%j==0){
count++;
}
}
if(count==2){
printf("%d ",i);
}
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter two number with intervals 20 80 Prime numbers 20 to 80 are: 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Display prime numbers between two intervals using while loop
In this program, we will print prime numbers between two intervals using a while loop in C language
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,min,max;
printf("Enter two number with intevals\n");
scanf("%d %d",&min,&max);
printf("Prime numbers %d to %d are\n",min,max);
i=min;
while(i<=max){
int count=0;
j=1;
while(j<=i){
if(i%j==0){
count++;
}
j++;
}
if(count==2){
printf("%d ",i);
}
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter two number with intervals 25 75 Prime numbers 25 to 75 are: 29 31 37 41 43 47 53 59 61 67 71 73
Display prime numbers between two intervals using do-while loop
In this program, we will print prime numbers between two intervals using a do-while loop in C language
Program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,min,max;
printf("Enter two number with intevals\n");
scanf("%d %d",&min,&max);
printf("Prime numbers %d to %d are\n",min,max);
i=min;
do{
int count=0;
j=1;
do{
if(i%j==0){
count++;
}
j++;
}while(j<=i);
if(count==2){
printf("%d ",i);
}
i++;
} while(i<=max);
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter two number with intervals 30 50 Prime numbers 30 to 50 are: 31 37 41 43 47
Display prime numbers between two intervals using function
In this program, we will print prime numbers between two intervals using a function in C language
Program 4
#include <stdio.h>
#include <stdlib.h>
int printPrime(int n);//function prototype
int main()
{
int i,flag,min,max;
printf("Enter two number with intervals\n");
scanf("%d %d",&min,&max);
printf("Prime numbers between %d to %d are\n",min,max);
for(i=min+1; i<max; i++){
flag=printPrime(i);//function call
if(flag==1){
printf("%d ",i);
}
}
getch();
return 0;
}
int printPrime(int n){//function definition
int j,flag=1;
for(j=2; j<=n/2; j++){
if(n%j==0){
flag=0;
break;
}
}
return flag;
}
When the above code is executed, it produces the following result
Enter two number with intervals 50 100 Prime numbers 50 to 100 are: 53 59 61 67 71 73 79 83 89 97
Suggested post
Similar post
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Code to print prime numbers from 1 to 100 or 1 to n in Java
Code to print prime numbers from 1 to 100 or 1 to n in C
Code to print prime numbers from 1 to 100 or 1 to n in C++
Code to print prime numbers from 1 to 100 or 1 to n in Python