Write a C++ Program to Display Prime Numbers Between Two Intervals Using Functions

C++ Program to Display Prime Numbers Between Two Intervals Using Functions

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

C++ Program to Display Prime Numbers Between Two Intervals Using Functions
C++ Program to Display Prime Numbers Between Two Intervals Using Functions

Write a C++ Program to Display Prime Numbers Between Two Intervals Using Functions

PROGRAM: C++ Program to Display Prime Numbers Between Two Intervals Using Functions

/* C++ Program to Display Prime Numbers Between Two Intervals Using Functions */

// C++ Program to Print Prime Numbers between two Numbers

#include <iostream>
bool isPrime(int);
void printPrime(int num1, int num2);

int main(){
int num1, num2;
std::cout << “Enter two positive integers: “;
std::cin >> num1 >> num2;
printPrime(num1, num2);
}
// prints the prime number between num1 and num2
void printPrime(int num1, int num2){
if (num1 > num2){
// swap if num1 is greater than num2
std::swap(num1, num2);
}
for(int i = num1 + 1 ; i < num2 ; ++i){
if (isPrime(i)){
std::cout << i << ” “;
}
}
}
// returns true is the given number is true false otherwise
bool isPrime(int n){
bool is_prime = true;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
return false;
}
for(int i = 2; i <= n/2; ++i) {
if (n % i == 0) {
return false;
}
}
return is_prime;
}

Leave a Reply

Your email address will not be published. Required fields are marked *