Anda di halaman 1dari 4

# C# Calculator

using System;

## public class Program

{
public static void Main()
{

char Operation;
double SUM, DIFF, PROD, QUOT, first_Value, second_Value;

Console.WriteLine("Arithmetic Operation");
Console.WriteLine("\t[S] for \"Subtraction\"");
Console.WriteLine("\t[M] for \"Multiplication\"");
Console.WriteLine("\t[D] for \"Division\"");

## Console.Write("Select the Arithmetic Operation: ");

if (Operation != 'A' && Operation != 'a' && Operation != 'S' && Operation != 's' && Operation !
= 'M' && Operation != 'm' && Operation != 'D' && Operation != 'd')
{
Console.WriteLine("That Operation is not part of the choices");
}

else
Console.Write("Enter the First Value: ");
Console.Write("Enter the Second Value: ");

## if (Operation == 'A' || Operation == 'a')

{
SUM = first_Value + second_Value;
Console.Write("The Sum of the two value is: " + SUM);
}
else if (Operation == 'S' || Operation == 's')
{
DIFF = first_Value - second_Value;
Console.Write("The Difference of the two value is: " + DIFF); }
else if (Operation == 'M' || Operation == 'm')
{
PROD = first_Value * second_Value;
Console.Write("The Product of the two value is: " + PROD); }
else if (Operation == 'D' || Operation == 'd')
{
QUOT = first_Value / second_Value;
Console.Write("The Quotient of the two value is: " + QUOT); }

}
}
Output
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form
{
Double value = 0;
String operation = "";
bool operation_pressed = false;

public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button_Click(object sender, EventArgs e)
{
if ((result.Text == "0")||(operation_pressed))
result.Clear();
Button b = (Button)sender;
result.Text = result.Text + b.Text;
}
private void button16_Click(object sender, EventArgs e)
{
result.Text = "0";
}
private void operator_click(object sender, EventArgs e)
{
Button b = (Button)sender;
operation = b.Text;
value = Double.Parse(result.Text);
operation_pressed = true;
}
private void button18_Click(object sender, EventArgs e)
{
switch (operation)
{
case "+":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value - Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value * Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text = (value / Double.Parse(result.Text)).ToString();
break;
default:
break;
}//endswitch
operation_pressed = false; }
}
}
Output