Suppose that the input is 10 -6 12 -5 -4 0. What is the output of the following code?
int num;
int sum = 0;
cin >> num;
while (num != 0)
{
if (num>0)
sum = sum + num;
else
sum = sum - num;
cin >> num;
}
cout << ” Sum = ” << sum << endl;
#include<iostream>
using namespace std;
int main(){
int num;
int sum = 0;
cin >> num;
while (num != 0)
{
if (num>0)
sum = sum + num;
else
sum = sum - num;
cin >> num;
}
cout << "Sum = " << sum << endl;
}
For input is: 10 -6 12 -5 -4 0,
Output is: 37
Comments
Leave a comment