What is output of the following code int num 12 while num

What is output of the following code?

int num = 12;

while ( num > = 0)

{

if (num% 5 ==0)

{

num++;

break;

}

cout << num << \" \";

num = num - 2;

}

cout << endl;

Solution

IterAtion 1 :
12>=0
if is false
Prints 12
num=12-2=10

Iteration 2:
10>=0
if is true
Num++
Num=11 is printed
Num=11-2=9

Iteration 3 :
9>=0
If is false
Prints 9
Num=9-2 =7

Iteration 4:
Same as iteration 3
Prints 7
Num=5

Iteartion 5:
5>=0
If is true
Num=5+1=6
Prints 6
Num=6-2=4

Iteration 6:
4>=0
If is false
Prints 4
Num=4-2=2

Iteration 7:
2>=0
If false
Prints 2
Num=2-2=0

Iteration 8:
0>=0
If is false
Prints 0
Num=0-2=-2

Output:
12
10
11
9
7
5
6
4
2
0

What is output of the following code? int num = 12; while ( num > = 0) { if (num% 5 ==0) { num++; break; } cout << num << \
What is output of the following code? int num = 12; while ( num > = 0) { if (num% 5 ==0) { num++; break; } cout << num << \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site