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

This section contains the C++ find output programs with their explanations on goto Statement (set 2).
Submitted by Nidhi, on June 04, 2020

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

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.

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

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.

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

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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.