Examples of C# statements

In this chapter you will learn:
In this chapter you will see some programming examples of C# statements that will help you to understand C# statements in details.
Qu 1:Write a program of calculating the percentage value of given number. After showing output asks user to whether continue or not. If continue, then again take input and shows the result. Hint: Use Goto label statement.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            int number, percentage, option;
            float result;

        label:
            Console.Write("\n\nEnter a number:\t");
            number = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nEnter Percentage Value:\t");
            percentage = Convert.ToInt32(Console.ReadLine());

            result = (float)(number * percentage) / 100;
            Console.WriteLine("Percentage value is:\t{0}", result);
            Console.Write("\nCalculate again press 1. To quit press digit:\t");
            option = Convert.ToInt32(Console.ReadLine());
            if (option == 1)
            {
                goto label;
            }
            Console.WriteLine("Press Enter for quit");
            Console.ReadLine();
        }
    }
}

 

Output


Enter a number: 342

Enter Percentage Value: 23
Percentage value is: 78.66

Calculate again press 1. To quit Press digit: 1
Enter a number: 732
Enter Percentage Value: 45
Percentage value is: 329.4
Calculate again press 1. To quit press digit: 6
Press Enter for quit
__

Summary

After completing programming examples of statements, next you will get someĀ  programming exercises of statements. You must do the exercises in order to improve your programming skills.

 

Share your thought