Question: In C++ 9. Rewrite the following code segment and two different ways, first using a while loop and then a do...while loop. Note that both
In C++
9. Rewrite the following code segment and two different ways, first using a while loop and then a do...while loop. Note that both must the same output as the following code segment:
int limit = 4; int first = 5; int j; for(j = 1; j <= limit; j++) {
cout << first * j << endl;
first = first + (j - 1);
}
cout << endl;
10. To learn how nested for loops work, do a walk-through of the following program segments and in each case determine the exact output.
Case one:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
Output:
Case two:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
Output:
Case three:
const int M = 5;
const int N = 5;
int i, j;
for (i = 1; i <= M; i++)
{
for (j = 1; j <= N; j++)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}
Output:
Case four:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= (5 - 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;
}
Output:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
