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.
#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;
}
Comments
Leave a comment