Do Arithmetic Operator according to user choice with Switch loop in C#

Hello Guys! Welcome To My Blog!

Do Arithmetic Operator according to user choice with Switch loop in C# 

Today we will be learn how we will be perform Arithmetic operation according to user choice
in this code we will be use switch loop and while loop .. 
While loop is optional if you want repeating program use this..
let see How its happens

Program to make Arithmetic Operation From User Choice

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arithmetic_Operator
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {


                Console.WriteLine("Enter Numbers To Perform Arithmetic Operation\n");

                Console.WriteLine("Enter First Number:");//put first number
                int num1 = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second Number:");//put Second number
                int num2 = int.Parse(Console.ReadLine());

                Console.WriteLine("What Action You Want To Perform Between In Two Numbers");
                Console.WriteLine("Addition: +\tSubtraction: -\tMultiplication: *\tDivide: / ");
                string action = Console.ReadLine();
                switch (action)
                {
                    case "+"://if user put "+" the addition operation will be done
                        Console.WriteLine("Adddition: " + (num1 + num2));
                        break;
                    case "-"://if user put "-" the subtraction operation will be done
                        Console.WriteLine("Subtraction: " + (num1 - num2));
                        break;
                    case "*"://if user put "*" the multiplication operation will be done
                        Console.WriteLine("Multiplication: " + (num1 * num2));
                        break;
                    case "/"://if user put "/" the divide operation will be done
                        Console.WriteLine("Divide: " + (num1 / num2));
                        break;
                    default:
                        { Console.WriteLine("Please Type Correct Input"); }//if user put anything else it show Please Type Correct Input
                        break;

                }
            }
            Console.ReadLine();
        }
    }
}

OUTPUT!

  • you can see on Highlight step it means if user put anything else it show Please Type Correct Input
  • The Program is Repeating because of While loop..

Comments