127k views
5 votes
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"

User A Sz
by
4.9k points

1 Answer

4 votes

Answer:

Following are the program to this question:

#include<iostream> //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

Step-by-step 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.
User CisSasGot
by
5.5k points