C++ Program to calculate factorial of a number using goto and Labels [DEVCPP/GCC]

STEPS

To calculate factorial of a number,

1. Initialize a variable (fact) with 1.
                     fact=1;

2. Multiply fact with given number(num).
                 fact = fact * num;

3. Decrement num by 1.
                      num--;

4. Repeat steps 2-3 until num>0.

PROGRAM

//Program to calculate factorial of a number using goto and Labels

#include <iostream>

using namespace std;

int main()
{
        int num ,n ;
        long long fact=1;

        cout<<"ENTER NUMBER: ";
        cin>>num;

        n=num;

        Top:                        //Label Top

                  if(num>0)
                 {
                        fact = fact * num ;
                        num--;
                        goto Top;            // Switches control to Label Top
                 }

        cout<<"FACTORIAL OF "<<n<<" IS "<<fact;

        return 0;
}

OUTPUT

C++ Program to calculate factorial of a number using goto and Labels with output

Share this

Related Posts

FIND US ON FACEBOOK!