Create a simple calculator using if else condition in which you will take two number as input in c#.
Create a simple calculator using if else condition in which you will take two number as input and ask user which operation he/she wants to perform.
CODE:
using System;
public class Simple Calculator
{
public static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine("{0}",dt);
Console.WriteLine("=====Simple Calculator=====");
int num1, num2;
Console.WriteLine("Enter First Number:");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number:");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("----Arithmetical Operaters----");
Console.WriteLine("1.Addition");
Console.WriteLine("2.Substraction");
Console.WriteLine("3.Multiplication");
Console.WriteLine("4.Division");
char choose;
Console.WriteLine("Which operation you want to choose:");
choose = Convert.ToChar(Console.ReadLine());
int result;
if (choose == '+')
{
result = num1 + num2;
Console.WriteLine("Sum of two number is:{0}", result);
}
else if (choose == '-')
{
result = num1 - num2;
Console.WriteLine("Substraction of two Number is:{0}", result);
}
else if (choose == '*')
{
result = num1 * num2;
Console.WriteLine("Multiclication of Number is:{0}", result);
}
else if (choose == '/')
{
if (num2 != 0)
{
result = num1 / num2;
Console.WriteLine("Division of Numbers is:{0}", result);
}
else
{
Console.WriteLine("Operation not possible with zero");
}
}
else
{
Console.WriteLine("Invalid Operation");
}
}
}
Comments
Post a Comment