int num int sum cin num sum num while num 1 sum sum 2
int num;
int sum;
cin >> num;
sum = num;
while (num != -1)
{
sum = sum + 2 * num;
cin >> num;
}
cout << \"Sum = \" << sum << endl;
Suppose that the input is 3 4 6 7 2 -1.
What is the output of the following code?
Suppose that the input is:
58 23 46 75 98 150 12 176 145 -999
What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num;
int count = 0;
cin >> num;
while (num != -999)
{
count++;
cout << num % count << \" \";
cin >> num;
}
cout << endl;
return 0;
}
Solution
1.Suppose that the input is 3 4 6 7 2 -1.
What is the output of the following code?
int num;
int sum;
cin >> num; // cin reads the user entered value.
sum = num; // num is assigned to sum.
while (num != -1)
{
sum = sum + 2 * num;
cin >> num;
}
cout << \"Sum = \" << sum << endl;
Suppose that the input is 3 4 6 7 2 -1.
OUTPUT & Explanation :-
Initially cin reads the user entered value as 3
Next the num is assigned to sum.
Further while (3 != -1) which is True.
Now sum = sum + 2 * num = 9.
Further cin reads the user entered value as 4.
Finally cout displays the result as 9.
Then the while loop will be continued until the will be failed.
Now while (4 != -1) which is True.
Now sum = sum + 2 * num = 17.
Further cin reads the user entered value as 6.
Finally cout displays the result as 17.
Then the while loop will be continued until the will be failed.
Now while (6 != -1) which is True.
Now sum = sum + 2 * num = 29.
Further cin reads the user entered value as 7.
Finally cout displays the result as 29.
Then the while loop will be continued until the will be failed.
Now while (7 != -1) which is True.
Now sum = sum + 2 * num = 43.
Further cin reads the user entered value as 2.
Finally cout displays the result as 43.
Then the while loop will be continued until the will be failed.
Now while (2 != -1) which is True.
Now sum = sum + 2 * num = 47.
Further cin reads the user entered value as -1.
Finally cout displays the result as 47.
Then the while loop will be continued until the will be failed.
Now while ( -1 != -1) which is False.
Then the while loop will be Terminated..
2.Suppose that the input is: 58 23 46 75 98 150 12 176 145 -999
What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num;
int count = 0; // variable count is assigned to 0.
cin >> num; // num is the user entered value which is stored in cin.
while (num != -999)
{
count++; // count++ increments the count value by 1.
cout << num % count << \" \"; // cout prints the result.
cin >> num; // cin reads the user entered value and is stored in cin.
}
cout << endl;
return 0;
}
OUTPUT & Explanation :-
Initially variable count is assigned to 0.
Then cin reads the user entered value as 58.
Further while (58 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 58%1 = 0.
Finally cout displays the result as 0.
Further cin reads the user entered value as 23.
Then the while loop will be continued until the will be failed.
Now while (23 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 58%2 = 0.
Finally cout displays the result as 0.
Further cin reads the user entered value as 46.
Then the while loop will be continued until the will be failed.
Now while (46 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 46%3 = 1.
Finally cout displays the result as 1.
Further cin reads the user entered value as 75.
Then the while loop will be continued until the will be failed.
Now while (75 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 75%4 = 3.
Finally cout displays the result as 3.
Further cin reads the user entered value as 98.
Then the while loop will be continued until the will be failed.
Now while (98 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 98%5 = 3.
Finally cout displays the result as 3.
Further cin reads the user entered value as 150.
Then the while loop will be continued until the will be failed.
Now while (150 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 150%6 = 0.
Finally cout displays the result as 0.
Further cin reads the user entered value as 12.
Then the while loop will be continued until the will be failed.
Now while (12 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 12%7 = 5.
Finally cout displays the result as 5.
Further cin reads the user entered value as 176.
Then the while loop will be continued until the will be failed.
Now while (176 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 176%8 = 0.
Finally cout displays the result as 0.
Further cin reads the user entered value as 145.
Then the while loop will be continued until the will be failed.
Now while (145 != -999) which is True.
Next count++ increments the variable count by 1.
Now num % count = 145%9 = 1.
Finally cout displays the result as 1.
Further cin reads the user entered value as -999.
Then the while loop will be continued until the will be failed.
Now while ( -999 != -999) which is False.
Then the while loop will be Terminated..
| num | Output & Explanation |
| 3 | Initially cin reads the user entered value as 3 Next the num is assigned to sum. Further while (3 != -1) which is True. Now sum = sum + 2 * num = 9. Further cin reads the user entered value as 4. Finally cout displays the result as 9. Then the while loop will be continued until the will be failed. |
| 4 | Now while (4 != -1) which is True. Now sum = sum + 2 * num = 17. Further cin reads the user entered value as 6. Finally cout displays the result as 17. Then the while loop will be continued until the will be failed. |
| 6 | Now while (6 != -1) which is True. Now sum = sum + 2 * num = 29. Further cin reads the user entered value as 7. Finally cout displays the result as 29. Then the while loop will be continued until the will be failed. |
| 7 | Now while (7 != -1) which is True. Now sum = sum + 2 * num = 43. Further cin reads the user entered value as 2. Finally cout displays the result as 43. Then the while loop will be continued until the will be failed. |
| 2 | Now while (2 != -1) which is True. Now sum = sum + 2 * num = 47. Further cin reads the user entered value as -1. Finally cout displays the result as 47. Then the while loop will be continued until the will be failed. |
| -1 | Now while ( -1 != -1) which is False. Then the while loop will be Terminated.. |




