0% found this document useful (0 votes)
54 views4 pages

Program: C# Calculator Using Public Class Public Static Void

The document describes a C# program that acts as a basic calculator, allowing the user to choose an arithmetic operation and then enter two values to perform the calculation, displaying the result. The program uses variables to store the operation chosen, input values, and results, and contains if/else statements to evaluate the operation and perform the appropriate calculation and output. The calculator program provides a simple way to add, subtract, multiply and divide two numbers in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Program: C# Calculator Using Public Class Public Static Void

The document describes a C# program that acts as a basic calculator, allowing the user to choose an arithmetic operation and then enter two values to perform the calculation, displaying the result. The program uses variables to store the operation chosen, input values, and results, and contains if/else statements to evaluate the operation and perform the appropriate calculation and output. The calculator program provides a simple way to add, subtract, multiply and divide two numbers in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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[A] for \"Addition\"");
Console.WriteLine("\t[S] for \"Subtraction\"");
Console.WriteLine("\t[M] for \"Multiplication\"");
Console.WriteLine("\t[D] for \"Division\"");

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


Operation = Convert.ToChar(Console.ReadLine());

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: ");
first_Value = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the Second Value: ");
second_Value = Convert.ToDouble(Console.ReadLine());

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); }

Console.ReadLine();
}
}
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

You might also like