Answer to Question #261087 in C++ for Charmaine

Question #261087

write a program executing a loop,which is readinginteger numbers from the keyboard until at least one of the below listed conditions is fulfilled

1.the multiplication of these numbers exceeds 1000

2.the number of reported even numbers exceeds 10

3.three subsequent numbers will have exactly the same value (eg 3,4,2,5,5,5)


1
Expert's answer
2021-11-04T08:38:11-0400
#include <iostream>
using namespace std;

int main() {
    int x;
    int product=1;
    int num_even=0;
    int prev;
    int num_prev=0;


    while (true) {
        cout << "Enter an integer: ";
        cin >> x;
        product *= x;
        if (product > 1000) {
            cout << "Product: " << product << " is greater than 1000" << endl;
            break;
        }

        if  (x % 2 == 0) {
            num_even++;
        }
        if (num_even > 10) {
            cout << "More than 10 even number" << endl;
            break;
        }

        if (num_prev == 0) {
            num_prev = 1;
            prev = x;
        }
        else {
            if ( prev == x) {
                num_prev++;
            }
            else {
                num_prev = 1;
                prev = x;
            }
        }
        if (num_prev == 3) {
            cout << "Meet " << prev << " three subsequent times" << endl;
            break;
        }
    }

    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
APPROVED BY CLIENTS