Q:

C++ goto Statement | Find output programs | Set 2

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on goto Statement (set 2).

Program 1:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int num = 5;
    int count = 1;

MY_LABEL:
    cout << count * num << " ";
    count++;
    if (count <= 10)
        goto MY_LABEL;

    return 0;
}

Program 2:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int num = 5;
    int result = 1;

MY_LABEL:
    result = num * result;
    num--;
    if (num > 0)
        goto MY_LABEL;

    cout << result;
    return 0;
}

Program 3:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int num1 = 5;
    int num2 = 3;
    int result = 1;

MY_LABEL:
    result = result * num1;
    num2--;
    if (num2 > 0)
        goto MY_LABEL;

    cout << result;
    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

5 10 15 20 25 30 35 40 45 50

Explanation:

The above code will print a table of 5 on the console screen. Here num is initialized with 5 and count is initialized with 1, count variable will increase till 10 and multiple by number in each iteration then it will print complete table of 5.

Answer Program 2:

Output:

120

Explanation:

The above code will print factorial of 5, here we initialize variable num by 5, and decrease the variable num from 5 to 1, Let's understand the flow,

num = 5, result=1 then
 result = 5*1 that is 5.

num = 4, result=5 then
 result =  4*5 that is 20.

num = 3, result=20 then
 result =  3*20 that is 60.

num = 2, result=60 then
 result =  2*60 that is 120

num = 1, result=120 then
 result =  1*120 that is 120

And then the condition will be false 
and then print result that is 120.

Answer Program 3:

Output:

125

Explanation:

The above code will print "125". Here we are calculating power 3 of number 5 that is 125.

Let's understand the program, here we took three variables num1, num2, and result that are initialized with 5, 3, and 1 respectively.

num1 = 5 , num2= 3 and result 1
 result = 1 * 5;
 =  5;
Decrease num2 by 1.

Condition is true then program control will be 
transferred at label MY_LABEL.
 
num1 = 5 , num2= 2 and result 5
 result = 5 * 5;
 =  25;
Decrease num2 by 1.

Condition is true then program control will be 
transferred at label MY_LABEL.

num1 = 5 , num2= 1 and result 25
 result = 25 * 5;
 =  125;
Decrease num2 by 1.
The condition will be true and then print result 
that is 125 on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ goto Statement | Find output programs | Set 2... >>
<< C++ goto Statement | Find output programs | Set 1...