C# Program to perform arithmetic operations using switch case


In this program, You will learn how to perform arithmetic operations using a switch case in C#.


switch (choice) { 
   //statement
}

Example: How to perform arithmetic operations using a switch case in C#.

using System;

public class Test
{
	public static void Main(string[] args)
	{
		int x, y, res;
		int ch;

		Console.WriteLine("Enter 1 For Addition :");
		Console.WriteLine("Enter 2 For Subtraction :");
		Console.WriteLine("Enter 3 For Multiplication :");
		Console.WriteLine("Enter 4 For Division :");
		Console.WriteLine("Enter 5 For Mode :");
		ch = Convert.ToInt32(Console.ReadLine());

		switch (ch)
		{
			case 1:
				{
					Console.WriteLine("Enter Two Numbers :");
					x = Convert.ToInt32(Console.ReadLine());
					y = Convert.ToInt32(Console.ReadLine());
					res = x + y;
					Console.WriteLine("Result is :" + res);
					break;
				}
			case 2:
				{
					Console.WriteLine("Enter Two Numbers :");
					x = Convert.ToInt32(Console.ReadLine());
					y = Convert.ToInt32(Console.ReadLine());
					res = x - y;
					Console.WriteLine("Result is :" + res);
					break;
				}
			case 3:
				{
					Console.WriteLine("Enter Two Numbers :");
					x = Convert.ToInt32(Console.ReadLine());
					y = Convert.ToInt32(Console.ReadLine());
					res = x * y;
					Console.WriteLine("Result is :" + res);
					break;
				}
			case 4:
				{
					Console.WriteLine("Enter Two Numbers :");
					x = Convert.ToInt32(Console.ReadLine());
					y = Convert.ToInt32(Console.ReadLine());
					res = x / y;
					Console.WriteLine("Result is :" + res);
					break;
				}
			case 5:
				{
					Console.WriteLine("Enter Two Numbers :");
					x = Convert.ToInt32(Console.ReadLine());
					y = Convert.ToInt32(Console.ReadLine());
					res = x % y;
					Console.WriteLine("Result is :" + res);
					break;
				}
		}
	}
}

Output:

Enter 1 For Addition :
Enter 2 For Subtraction :
Enter 3 For Multiplication :
Enter 4 For Division :
Enter 5 For Mode :
1
Enter Two Numbers :
10
5     
Result is :15