C# program to find factorial of number using goto loop.

Introduction:

In post related to the Factorial Program we have seen how to implement Factorial program using For Loop and Do-While Loop. Now in this post we are going to take a look on one interesting program in which  we will try to implement Factorial Program using goto loop.It will be fun to implement this program once we are familiar to goto. So before moving further we will check out what is goto.

Background:

Before implementing this program you must be  familiar with goto loop and the concept of factorial.

goto: 

goto is an unconditional jump statement in programming.It would jump to particular location marked as label and then continue the  execution of  the program for there.We can use goto where we want to skip some line of code and continue from some other location.But i would not advise the use of goto in large programs, because it makes difficult to know the flow of program. 



1
2
3
4
5
Syntax: goto label;
        ..........
        ..........
        ..........
        label:statements;

To know more follow the links:
Reference for goto: MSDN | DotNET Perls
                       Factorial: Math is Fun

Implementation:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int fact = 1;
            Console.WriteLine("Enter Number");
            i = Convert.ToInt16(Console.ReadLine());

            Factorial:
            

               fact = fact * i;
               i=i-1;

               while(i!=0)
                {
                    goto Factorial;
                }
            Console.WriteLine(fact);
            }
        }
    }

Comments