Write pseudo-code of program that finds “Prime Numbers” in the given range. First, ask the user to enter two
numbers M and N. Then, find prime numbers in the range M
→
N. Display all the prime numbers on the
screen. by using nested loop
#include <iostream>
using namespace std;
int main() {
int M, N, i;
bool is_prime = true;
cout << "Enter two numbers (intervals): ";
cin >> M >> N;
cout << "\nPrime numbers between " << M << " and " << N << " are: " << endl;
while (M < N) {
is_prime = true;
// 0 and 1 are not prime numbers
if (M == 0 || N == 1) {
is_prime = false;
}
for (i = 2; i <= M / 2; ++i) {
if (M % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << M << " ";
++M;
}
return 0;
}
Comments
Leave a comment