Ask Question
6 October, 08:54

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"

+3
Answers (1)
  1. 6 October, 09:14
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers