In this tutorial, we will learn to write the C Program to print Prime Number in a given range.
Let’s see what is our problem statement which we will solve in this tutorial.
We will take the range in the form of an integer. After taking the input we will then find all the prime numbers existing in the given range.
For Example:
The input we have taken 3 and 15
So, the output will be 5, 7, 11 and13. Because these are the only numbers that are prime between the range 3 and 15.
How our program will behave
- Our Program will take two integer numbers as an input to find the find the prime numbers.
- Now we will use while loop start from first integer till smaller than the second input. This is because we only want in between two.
- We will pick every time one number and will check it is divisible by any number other than 1.
- If it is divisible then it is not a prime number. Otherwise our program will print that number as Prime number.
C Program to print Prime Number in a Given range
#include<stdio.h>
void main() {
int i = 0, fNum, sNum, temp;
printf("C Program to Print Prime number in a given range\n");
printf("Please give the first number: ");
scanf("%d",&fNum);
printf("Please give the second number: ");
scanf("%d",&sNum);
printf("Prime numbers between %d and %d are\n",fNum,sNum );
while (fNum<=sNum) {
temp = 0;
for (i = 2;i<=(fNum/2);i++) {
if (fNum%i == 0) {
temp=1;
break;
}
}
if (temp == 0)
printf("%d \n", fNum);
fNum++;
}
}
Output
Please give the first number: 12
Please give the second number: 34
Prime numbers between 12 and 34 are
13
17
19
23
29
31
[wpusb]
Also Prepare Below Important Question
- Sum of digits of Given Number in Java
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions