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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.