q8) CONSIDER THE FOLLOWING CODE: void Question() { int i, j; for (i = 1; i <= 4;
ID: 3810039 • Letter: Q
Question
q8)
CONSIDER THE FOLLOWING CODE:
void Question()
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{
if (i <= j)
cout << " " << j*i;
else
cout << " " << "*";
}
cout << endl;
}
}
Select one:
a. 1 2 3 4
* * * *
* * * *
* * * *
b. 1 2 3 4
4 6 8 8
9 12 12
16 * * *
c. 1 2 3 4
* 4 6 8
* * 9 12
* * * 16
d. 1 * * 4
* 4 6 8
* * 9 12
* * * 16
please explain how you get the answer. Thank you
Explanation / Answer
void Question()
{
int i, j;
for (i = 1; i <= 4; i++) //This loop starts from i = 1 to 4.
{
for (j = 1; j <= 4; j++) //This loop starts from j = 1 to 4.
{
if (i <= j) //If i <= j, (i.e., for upper triangular values.)
cout << " " << j*i; //Will print the value(integer) j * i.
else
cout << " " << "*"; //For all other values, will just print a *.
}
cout << endl; //Move on to next line.
}
}
//Therefore, the output is:
1 2 3 4
* 4 6 8
* * 9 12
* * * 16
Therefore, the answer is:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.