C# Program to Demonstrate the Goto Statement


This is a C# Program to illustrate the concept of goto.

Problem Description

This C# Program Illustrates the Concept of Goto.

Problem Solution

Here the goto statement transfers the program control directly to a labeled statement that is Loop.

Program/Source Code

Here is source code of the C# Program to Illustrate the Concept of Goto. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Illustrate the Concept of Goto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
    class Program
        static void Main(string[] args)
            int no, per, option;
            float ans;
            Console.Write("Enter a Number :\t");
            no = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter Percentage Value : \t");
            per = Convert.ToInt32(Console.ReadLine());
            ans = (float)(no * per) / 100;
            Console.WriteLine("Percentage Value is:\t{0}", ans);
            Console.Write("\nCalculate again press 1.   To quit press digit:\t");
            option = Convert.ToInt32(Console.ReadLine());
            if (option == 1)
                goto loop;
            Console.WriteLine("Press Enter for quit");
Program Explanation

In this C# program we are reading a number and percentage value using ‘no’ and ‘per’ variables respectively. The ‘ans’ variable is used to compute the multiplication of the value of ‘no’ and ‘per’ variable and divide the value by 100. To continue the process we are entering the value as 1 to option variable.

Note: Join free Sanfoundry classes at Telegram or Youtube

If condition statement is used to check the value of ‘option’ variable is equal to 1. If the condition is true then execute the statement. The goto statement is used to transfer the program control directly to a labeled statement and print the value.

Runtime Test Cases
Enter a number : 320
Enter Percentage Value : 10
Percentage value is : 32
Calculate again press 1. To quit Press digit: 1
Enter a number : 730
Enter Percentage Value: 10
Percentage value is: 73
Calculate again press 1. To quit press digit: 6
Press Enter for quit

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.