You are on page 1of 4

Q2.

Create a program c# to perform switch case where user will have


four choice : 1 to add ,2 to sub, 3 to multiple and 4 to division (CO-1,
BL-6).
Solution:
using System;
class Program
{
public static void Main(string[] args)
{
int a, b, c, ch;
Console.WriteLine("Enter the first number ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your choice 1- Add 2-Sub 3-Multi 4-Divi");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1:
c = a + b;
Console.WriteLine("Add is " + c);
break;
case 2:
c = a - b;
Console.WriteLine("Sub is " + c);
break;
case 3:
c = a * b;
Console.WriteLine("Multi is " + c);
break;
case 4:
if (b != 0)
{
c = a / b;
Console.WriteLine("Div is " + c);
}
else
{
Console.WriteLine("Division by zero error!");
}
break;
default:
Console.WriteLine("You entered the wrong choice");
break;
}
Console.WriteLine("Created by Suraj Nath");
Console.WriteLine("MCA-II B, Roll No: 76");
Console.ReadKey();
}
}

Output:
Q2. Create a program c# to perform switch case where user will have
four choice : 1 to add ,2 to sub, 3 to multiple and 4 to division (CO-1,
BL-6).
Solution:
using System;
class Program
{
public static void Main(string[] args)
{
int a, b, c, ch;
Console.WriteLine("Enter the first number ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your choice 1- Add 2-Sub 3-Multi 4-Divi");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1:
c = a + b;
Console.WriteLine("Add is " + c);
break;
case 2:
c = a - b;
Console.WriteLine("Sub is " + c);
break;
case 3:
c = a * b;
Console.WriteLine("Multi is " + c);
break;
case 4:
if (b != 0)
{
c = a / b;
Console.WriteLine("Div is " + c);
}
else
{
Console.WriteLine("Division by zero error!");
}
break;
default:
Console.WriteLine("You entered the wrong choice");
break;
}
Console.WriteLine("Created by Sumit Singh Bisht");
Console.WriteLine("MCA-II B, Roll No: 75");
Console.ReadKey();
}
}

Output:

You might also like