Answer to Question #318803 in C++ for Zaki

Question #318803

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

1
Expert's answer
2022-03-26T18:45:18-0400
#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;
}

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

LATEST TUTORIALS
New on Blog