0% found this document useful (0 votes)
8 views3 pages

Topic 4 - Repetition Exercise

The document provides outputs for various C++ code snippets involving loops and conditional statements. It explains the results of the code executions, detailing the values of variables at each step. The final outputs include 'y = 1 and count = 100', '5', and 'Sum = 158' for different code examples.

Uploaded by

2023406116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Topic 4 - Repetition Exercise

The document provides outputs for various C++ code snippets involving loops and conditional statements. It explains the results of the code executions, detailing the values of variables at each step. The final outputs include 'y = 1 and count = 100', '5', and 'Sum = 158' for different code examples.

Uploaded by

2023406116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Topic 4 – Repetition Exercise

1.
What is the output of the following C++ code?
int count = 1;
int y = 100;
while (count < 100)
{
y = y - 1;
count++;
}
cout << " y = " << y << " and count = " << count << endl;
Y=100, count =1 Y=99
Count=2
Y=99, count =2 Y=98
Count =3
Y=98, count =3 Y=97
Count =4
….

Y=2, count= 99 Y=1


Count =100
Y= 1, count =100

Answer:
Y = 1 and count = 100

2.
What is the output of the following C++ code?
int num = 5;
while (num > 5)
num = num + 2;
cout << num << endl;
Num= 5 5

3.
What is the output of the following C++ code?
int num = 1;
while (num < 10)
{
cout << num << " ";
num = num + 2;
}
cout << endl;
Num =1 1
Num =3
Num =3 3
Num =5
Num =5 5
Num =7
Num =7 7
Num =9
Num =9 9
Num =11
Num =11 13579
4.
Suppose that the input is 38 35 71 14 -1. What is the output
of the following code? Assume all
variables are properly declared.
cin >> num;
sum = num;
while (num != -1)
{
cin >> num;
sum = sum + num;
}
cout << "Sum = " << sum << endl;
Num = 38 Sum =38
38!= -1(true)

35
Sum = 38+35
Sum = 73
Num = 35 35!= -1(true)

71
Sum = 73+71
Sum = 144
Num = 71 71!= -1(true)

14
Sum = 144+ 14
Sum = 158
Num = 14 14!= -1(true)

-1
Sum = 158 +(-1)
Sum = 157
Num = -1 Sum =157

5.

Suppose that the input is 38 35 71 14 -1. What is the output


of the following code? Assume all
variables are properly declared.
sum = 0;
cin >> num;
while (num != -1)
{
sum = sum + num;
cin >> num;
}
cout << "Sum = " << sum << endl;
Num = 38 38!= -1 (true)
Sum = 0+38
Sum = 38

35
Num = 35 35!= -1 (true)
Sum = 38 + 35
Sum = 73

71
Num = 71 71!= -1 (true)
Sum = 73+71
Sum = 144

14
Num = 14 14!= -1 (true)
Sum = 144+14
Sum = 158

-1
Num = -1 -1!= -1 (false)

Sum = 158

You might also like