Ask Question
22 December, 21:50

Initialize an int variable named as sum to 0. Write the while loop that lets the user enter a number. The number should be added to sum and the result is stored back to the variable sum. The loop should iterate as long as sum contains a value less than 200. After the loop stop, display message: "sum = - the loop stops"

+2
Answers (1)
  1. 22 December, 22:00
    0
    Following are the program to this question:

    #include / /defining header file

    using namespace std;

    int main () / /defining main method

    {

    int sum = 0, num; / /defining integer variable

    cout << "Enter a number : "; / / print message

    cin >> num; / /input number

    sum=sum+num;

    while (sum < 200) / /defining loop that check sum value is less then 200

    {

    cin >> num; / /input number by user

    sum = sum+num; / /add values

    }

    cout << "Sum = " << sum <<" the loop stops"; / /print values

    return 0;

    }

    output:

    Enter a number : 44

    66

    90

    Sum = 200 the loop stops

    Explanation:

    In the given C+ + program, the main method is declared, inside the method two integer variable "sum and num" is declared, in which sum variable assign a value, that is 0, and num is used to take input from the user end.

    In the next step, a sum variable adds num variable value and use a while loop that checks value is less than 200. In this condition is not true so, inside the loop, it takes value from the user and into the sum variable when its value is above 200, it will exit the loop and prints its total value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Initialize an int variable named as sum to 0. Write the while loop that lets the user enter a number. The number should be added to sum and ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers