Answer to Question #155332 in C++ for zain ul abdeen

Question #155332

Write a program which takes two integers (num1 and num2) as input from the user. The program should display all the prime numbers between num1 and num2. i) The program can use only for loops.


1
Expert's answer
2021-01-14T16:25:02-0500
#include <iostream>
#include <cmath>


bool isPrime(int n) 
{ 
    // Corner case 
    if (n <= 1) 
        return false; 
  
    // Check from 2 to n-1 
    for (int i = 2; i < n; i++) 
        if (n % i == 0) 
            return false; 
  
    return true; 
}


using namespace std;
int main () {
    int num1, num2;
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    cout << "The number1 must be less than number2\n";
    cout << "All prime numbrs between " << num1 << " and " << num2 << " : " << endl;
    for (int i = num1; i < num2; i++) {
        if (isPrime(i)) {
            cout << i << " ";
        }
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment