Objective:
Create a simple four
function calculator class.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ABC
{
class Calculator
{
double addition;
double subtraction;
double multiplication;
double division;
double remainder;
double a, b;
char op;
public void GetData()
{
Console.WriteLine("Enter
first value");
a
= Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter
operator");
op = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Enter
second value");
b
= Convert.ToDouble(Console.ReadLine());
}
public void Display()
{
switch (op)
{
case '+':
addition =Convert.ToDouble( a + b);
Console.WriteLine("Addition of {0} and {1} is {2}", a,
b,addition);
break;
case '-':
subtraction = Convert.ToDouble(a -
b);
Console.WriteLine("subtraction of {0} and {1} is {2}", a,
b,subtraction);
break;
case'*':
multiplication = Convert.ToDouble(a * b);
Console.WriteLine("Multiplication of {0} and {1} is {2}",a,b,multiplication);
break;
case'/':
division = Convert.ToDouble(a / b);
Console.WriteLine("Division of {0} and {1} is {2}",a,b,division);
break;
case'%':
remainder = Convert.ToDouble(a % b);
Console.WriteLine("Remainder is {0}",remainder);
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
class program
{
static void Main(string[]
args)
{
Calculator p1 = new Calculator();
p1.GetData();
p1.Display();
}
}
}
}
Output:
No comments:
Post a Comment