52.3k views
0 votes
4. What is the output of the following C++ code?

int num = 1;
while (num * num <= 50)
{
cout << num << " ";
num = num + 2;
}
cout << endl;

User Sentary
by
3.9k points

1 Answer

5 votes

Answer:

3 5 7 9

Step-by-step explanation:

First, integer 1 is stored in variable num.

In the while statement, the condition is 1*1 <=50 (Which is true)

If that condition is met, we add 2 to variable num, resulting in 3.

The condition fails at 8 because 8*8 is 64, but the condition is met at 7*7 which is 49 and then we add 2 in variable num, resulting in 9.

User Ramblinjan
by
4.2k points