- 0
Notifications Mark All Read
- Login
- Get Prime
Program to Display Prime Numbers Between Interval
Prime Number :
Prime Number is defined as the number which is divisible by itself and by one only. It should not be divisible by any other number .
Working of Program :
In the program, we will take two value and write function to check which number is prime or not between the two values.
Run
#include<stdio.h> // function to check number is prime or not int prime_check(int n){ for(int i=2;i < n;i++){ if(n%i==0) return 0; } return 1; } int main() { int val1 = 5, val2 = 50; printf("Prime number between intervals : "); for(int i = val1; i<=val2; i++){ if(prime_check(i) == 1){ printf("%d ", i); } } return 0; }
Output :
Prime number between intervals : 5 7 11 13 17 19 23 29 31 37 41 43 47
What Happened Above?
In the Above Program, we write a function prime_check to check whether the individual number is prime or not between the given interval.
Example :
Let’s solve the same problem by using the recursive function to check number whether prime instead of iterative functions.
Run
#include<stdio.h> // function to check number is prime or not int prime_check(int i, int n){ if(n==i) return 0; else if(n%i==0) return 1; else return prime_check(i+1,n); } int main() { int val1 = 5, val2 = 50; printf("Prime number between intervals : "); for(int i = val1; i<=val2; i++){ if(prime_check(2,i) == 0){ printf("%d ", i); } } return 0; }
Output :
Prime number between intervals : 5 7 11 13 17 19 23 29 31 37 41 43 47
What Happened Above?
In the above program,we write a recursive function prime_check to check individual number prime or not in the given interval.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment