prime numbers between two intervals
Table of Contents
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
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
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
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
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.