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++;

continue;

}

cout << num << \" \";

num = num - 2;

}

cout << endl;

Solution

Given code :-

#include <iostream>
using namespace std;

int main ()
{
  
int num = 12;
while ( num >= 0)
{
if (num% 5 ==0)
{
num++;
continue;
}
cout << num << \" \";
num = num-2;
}
cout << endl;
return 0;
}

OUTPUT :-

Explanation :-

STEP1 :-

Initially num = 12.

while ( 12 >= 0) // TRUE
{
if ( 12 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 12.
num = num-2; // NOW num BECOMES 12-2=10.
}

STEP2 :-

Now num = 10.

while ( 10 >= 0) // TRUE
{
if ( 10 % 5 ==0) // TRUE
{
num++; // num++ increments the num as 11.
continue; // continue statement forces the next iteration of the while-loop
}

STEP3 :-

Now num = 11.

while ( 11 >= 0) // TRUE
{
if ( 11 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 11.
num = num-2; // NOW num BECOMES 11-2=9.
}

STEP4 :-

Now num = 9.

while ( 9 >= 0) // TRUE
{
if ( 9 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 9.
num = num-2; // NOW num BECOMES 9-2=7.
}

STEP5 :-

Now num = 7.

while ( 7 >= 0) // TRUE
{
if ( 7 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 7.
num = num-2; // NOW num BECOMES 7-2=5.
}

STEP6 :-

Now num = 5.

while ( 5 >= 0) // TRUE
{
if ( 5 % 5 ==0) // TRUE
{
num++; // num++ increments the num as 6.
continue; // continue statement forces the next iteration of the loop
}

STEP7 :-

Now num = 6.

while ( 6 >= 0) // TRUE
{
if ( 6 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 6.
num = num-2; // NOW num BECOMES 6-2=4.
}

STEP8 :-

Now num = 4.

while ( 4 >= 0) // TRUE
{
if ( 4 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 4.
num = num-2; // NOW num BECOMES 4-2=2.
}

STEP9 :-

Now num = 2.

while ( 2 >= 0) // TRUE
{
if ( 2 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 2.
num = num-2; // NOW num BECOMES 2-2=0.

}

STEP10 :-

Now num = 0.

while ( 0 >= 0) // TRUE
{
if ( 0 % 5 ==0) // TRUE
{
num++; // num++ increments the num as 1.
continue; // continue statement forces the next iteration of the loop

}

STEP11 :-

Now num = 1.

while ( 1 >= 0) // TRUE
{
if ( 1 % 5 ==0) // FALSE
{
num++;
continue;
}
cout << num << \" \"; // cout prints the num as 1.
num = num-2; // NOW num BECOMES 1-2=(-1).

}

STEP12 :-

Now num = -1.

while ( -1 >= 0) // FALSE which means loop will be terminated.
{
if ( 2 % 5 ==0)   
{
num++;
continue;
}
cout << num << \" \";
num = num-2;

}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site