1. What is the output of the following C++ code? int i = 2; int temp = 1; while
ID: 3800105 • Letter: 1
Question
1. What is the output of the following C++ code?
int i = 2;
int temp = 1;
while (i < 10)
{
temp = temp * (i - 1);
i = i + 1;
}
cout << "i = " << i << " and temp = " << temp << endl;
2. Suppose that the input is 1 20 35 15 28 66 -1. What is the output of the following code? Is this a count-controlled loop or a sentinel-controlled loop?
3. Suppose that the input is 3 4 6 7 2 -1. What is the output of the following code? Is this a count-controlled loop or a sentinel-controlled loop?
int num;
int sum;
cin >> sum;
num = sum;
while (num != -1)
{
cin >> num;
sum = sum + 2 * num;
}
cout << "Sum = " << sum << endl;
4. When does the following while loop terminate?
ch = 'D';
while ('A' <= ch && ch <= 'Z')
ch = static_cast<char>(static_cast<int>(ch) + 1);
5. The following program is designed to input two numbers and output their sum. It asks the user if he/she would like to run the program. If the answer is Y or y, it prompts the user to enter two numbers. After adding the numbers and displaying the results, it again asks the user if he/she would like to add more numbers. However, the program fails to do so. Correct the program so that it works properly.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char response;
double num1;
double num2;
cout << "This program adds two numbers." << endl;
cout << "Would you like to run the program: (Y/y) ";
cin >> response;
cout << endl;
cout << fixed << showpoint << setprecision(2);
while (response == 'Y' && response == 'y')
{
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << endl;
cout << num1 << " + " << num2 << " = " << (num1 - num2) << endl;
cout << "Would you like to add again: (Y/y) ";
cin >> response;
cout << endl;
}
return 0;
}
6. What is the output of the following program segment?
int num = 1;
int i;
for (i = 0; i < 5; i++)
{
num = num * (5 - i);
cout << num << " ";
}
cout << endl;
7. Assume that the following code is correctly inserted into a program:
int s = 0;
int i;
for (i = 0; i < 5; i++)
{
s = 2 * s + i;
cout << s << " ";
}
cout << endl;
a. What is the final value of s?
(i) 11 (ii) 4 (iii) 26 (iv) none of these
b. If a semicolon is inserted after the right parenthesis in the for loop statement, what is the final value of s?
(i) 0 (ii) 1 (iii) 2 (iv) 5 (v) none of these
c. If the 5 is replaced with a 0 in the for loop control expression, what is the final value of s?
(i) 0 (ii) 1 (iii) 2 (iv) none of these
8. What is the output of the following code? Is there a relationship between the variables x and y? If yes, state the relationship? What is the output?
int x = 19683;
int i;
int y = 0;
for (i = x; i >= 1; i = i / 3)
y++;
cout << "x = " << x << ", y = " << y << endl;
9. Use a do..while loop to write an input statement validation loop that prompts the user to enter a number less than 20 or greater than 75.
10. Which of the following apply to the while loop only? To the do...while loop only? To both?
It is considered a conditional loop.
The body of the loop executes at least once.
The logical expression controlling the loop is evaluated before the loop is entered.
The body of the loop may not execute at all.
11. Rewrite the following as a for loop:
int i = 0, value = 0;
while (i <= 20)
{
if (i % 2 == 0 && i <= 10)
value = value + i * i;
else if (i % 2 == 0 && i > 10)
value = value + i;
else
value = value - i;
i = i + 1;
}
cout << "value = " << value << endl;
What is the output of this loop?
Rewrite the while loop as a do...while loop
12. To learn how nested for loops work, do a walk-through of the following program segments and determine, in each case, the exact output.
a. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
cout << setw(3) << i;
cout << endl;
}
b. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
c. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
d. const int M = 10;
const int N = 10;
int i, j;
for (i = 1; i <= M; i++)
{
for (j = 1; j <= N; j++)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}
e. int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= (9 - i); j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << setw(1) << j;
for (j = (i - 1); j >= 1; j--)
cout << setw(1) << j;
cout << endl;
}
13. What is the output of the following code?
int num = 12;
while (num >= 0)
{
if (num % 5 == 0)
break;
cout << num << " ";
num = num - 2;
}
cout << endl;
14. What is the output of the following code?
int num = 12;
while (num >= 0)
{
if (num % 5 == 0)
{
num++;
continue;
}
cout << num << " ";
num = num - 2;
}
cout << endl;
Explanation / Answer
Answer to question 1:
Output of the program will be printed by cout statement.
When the loop is completed value of i will be 10 and value of temo will be 1*2*3*4*5*6*7*8 as the comutation happens until i is 9 and it multiplies by the previous number.'
As 1*2*3*4*5*6*7*8 = 40320, the output is
i = 10 and temp = 40320
The code can alsp be pasted in a c compiler and executed to check the output for this question and some of the other questions above.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.