C program to display prime numbers between two intervals
In this post, you will learn how to display the Prime numbers between two intervals using the C programming language.
A prime number is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11 etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9 and the number itself.
C program to display prime numbers between two intervals using for loop
In the given example, we have used for loop to display the prime numbers between two intervals. In this, we declare a flag variable to check whether the number is prime or not with the help of for loop condition. When, the flag is zero, it prints the prime number and if flag is one, it exists from the loop.
#include <stdio.h>
int main(){
int num1,num2,i,j,flag;
printf("Enter the two intervals:");
scanf("%d %d",&num1,&num2);
printf("Prime Numbers between %d and %d:",num1,num2);
for(i=num1+1;i<num2;i++){
flag=0;
//checking number is prime or not
for(j=2;j<=i/2;++j){
if(i%j==0){
flag=1;
break;
}
}
if(flag==0)
printf("%d\n",i);
}
return 0;
}
Output of the above code:
Enter the two intervals:1 50
Prime Numbers between 1 and 50:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
C program to display prime numbers between two intervals using while loop
In the given C program, we have used the while loop to display the prime numbers between two intervals.
#include<stdio.h>
int main() {
int num1, num2, i, flag;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Prime numbers between %d and %d are: ", num1, num2);
while(num1 < num2) {
flag = 0;
if (num1 <= 1) {
++num1;
continue;
}
for (i = 2; i <= num1 / 2; ++i) {
if (num1 % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", num1);
++num1;
}
return 0;
}
Output of the above code:
Enter two numbers: 10 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47
C program to display prime numbers between two intervals using function
Here, we have defined a function displayPrimeNumber() to display prime numbers between two intervals.
#include <stdio.h>
int displayPrimeNumber(int x);
int main ()
{
int num1, num2, i, flag;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Prime numbers between %d and %d:\n", num1, num2);
// getting prime number between num1 and num2
for(i = num1 + 1; i < num2; ++i)
{
flag = displayPrimeNumber(i);
if (flag == 0)
printf("%d\t", i);
}
return 0;
}
int displayPrimeNumber(int x)
{
int y, flag = 0;
for(y = 2; y <= x/2; ++y)
{
if (x % y == 0)
{
flag = 1;
break;
}
}
return flag;
}
Output of the above code:
Enter two numbers: 20
80
Prime numbers between 20 and 80:
23 29 31 37 41 43 47 53 59 61 67 71 73 79
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative