Answer to Question #162369 in C++ for Aarati

Question #162369

int n, divisor = 2; cin >> n;

bool divisorFound = false;

repeat(n - 2) {

if (n % divisor == 0) {

divisorFound = true;

BlankZ ;

}

divisor++;

}

if (!divisorFound) cout << "Prime\n";

else cout << "Composite\n";

There is no need to iterate further once we have encountered a divisor of the number. What should be the command to be replaced by BlankZ?


Mention a lowercase character string as the answer.


1
Expert's answer
2021-02-09T15:26:53-0500
#include <iostream>
#define repeat(n) for (int i = 0; i < n; i++) 
using namespace std;
int main () {
    int n, divisor = 2;
    cout << "Enter a number: "; cin >> n;
    bool divisorFound = false;
    repeat (n - 2) {
        if (n % divisor == 0) {
            divisorFound = true;
        }
        divisor++;
    }
    if (!divisorFound) cout << "Entered number is Prime\n";
    else cout << "Entered number is Composite\n";
    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