Answer to Question #319647 in C++ for Sami khattak

Question #319647

Come up with the remaining loop , flow , and example on each one .

1.while loop

2.do while loop

3.infinite loop


1
Expert's answer
2022-03-28T13:52:33-0400
#include <iostream>
using namespace std;


int main() {
    cout << "While loop: sum all numbers from 1 till the sum is less than N" << endl;
    int i, sum, n;


    cout << "Enter N: ";
    cin >> n;
    sum = 0;
    i = 1;
    while (sum < n) {
        sum += i;
        cout << "i = " << i << " sum = " << sum << endl;
        i++;
    }
    cout << "Done!" << endl;


    cout << endl << "do while loop" << endl;
    char ans;


    do {
        cout << "Do you want to see this message ones more? (Y/n) ";
        cin >> ans;
    } while (ans != 'n');



    cout << endl << "WARNING! Infinit loop (to terminate press Ctrl-C)" << endl;
    int x=1;
    while (x) {
        x = 1;
    }
    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