Blogger Widgets

Total Page visits

Wednesday, April 24, 2013

Simple calculator using Switch Statement in C#



using System;
class Simcalc
{
public static void Main(String[] args)
{
double a,b,rpt=1;
int choice;
while(rpt!=0)
{
Console.WriteLine(“Select the operation”);
Console.WriteLine(“1 . Addition”);
Console.WriteLine(“2 . Subtraction”);
Console.WriteLine(“3 . Multiplication”);
Console.WriteLine(“4 . Division”);
Console.WriteLine(“0 . Exit”);
Console.WriteLine(“Enter ur choice :”);
choice=int.Parse(Console.ReadLine());
switch(choice)
{
case 1:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Addition : “+(a+b));
break;
case 2:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Subtraction : “+(a-b));
break;
case 3:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Multiplication : “+(a*b));
break;
case 4:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
if(b==0)
{
Console.WriteLine(“Division not possible”);
}
else
{
Console.WriteLine(“Result of Division : “+(a/b));
}
break;
case 0:
rpt=0;
break;
default:
Console.WriteLine(“Invalid selection”);
break;
}
}
}
}

No comments: