In: Computer Science
This is to check to ensure you are current in your work.
Please create a simple calculator with only +, - and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions:
+, -, divide and multiply.
Please have the answers, always rounded to two decimal figures. The calculator will also have to be able to take up to 5 values.
Hi,
I have created a Calculator Project using C# Form
Below is the Form Details:
using System;
using System.Windows.Forms;
namespace NestedLoop
{
public partial class Calculator : Form
{
public Calculator()
{
InitializeComponent();
}
/// <summary>
/// Addition of 2 numbers
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
//Check Validations
if (txtNumber1.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number1";
}
else if (txtNumber2.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number2";
}
else
{
//Clear the Error message string
lblErrorMessage.Text = string.Empty;
//Fetch the 2 numbers
int iNumber1 = Convert.ToInt32(txtNumber1.Text);
int iNumber2 = Convert.ToInt32(txtNumber2.Text);
//Apply Addition logic
int outPut = iNumber1 + iNumber2;
//Set result to the final text
txtNumber3.Text = outPut.ToString();
}
}
catch (Exception ex)
{
throw (ex);
}
}
/// <summary>
/// Substraction of 2 numbers
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSubstraction_Click(object sender, EventArgs e)
{
try
{
//Check Validations
if (txtNumber1.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number1";
}
else if (txtNumber2.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number2";
}
else
{
//Clear the Error message string
lblErrorMessage.Text = string.Empty;
//Fetch the 2 numbers
int iNumber1 = Convert.ToInt32(txtNumber1.Text.Trim());
int iNumber2 = Convert.ToInt32(txtNumber2.Text.Trim());
//Apply substraction logic
int outPut = iNumber1 - iNumber2;
//set result to the final text
txtNumber3.Text = outPut.ToString();
}
}
catch (Exception ex)
{
throw (ex);
}
}
/// <summary>
/// Division of 2 numbers
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDivide_Click(object sender, EventArgs e)
{
try
{
//Check Validations
if (txtNumber1.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number1";
}
else if (txtNumber2.Text.Trim().Length <= 0)
{
lblErrorMessage.Text = "Please enter Number2";
}
else if (Convert.ToInt32(txtNumber2.Text.Trim()) <= 0)
{
lblErrorMessage.Text = "Number2 should be greater than 0";
}
else
{
//Clear the Error message
lblErrorMessage.Text = string.Empty;
//Fetch the 2 numbers
decimal iNumber1 = Convert.ToDecimal(txtNumber1.Text.Trim());
decimal iNumber2 = Convert.ToDecimal(txtNumber2.Text.Trim());
//Do the Division of 2 numbers
decimal ab = new decimal();
ab = Math.Round((decimal)(iNumber1 / iNumber2), 2);
//set result to the final text
txtNumber3.Text = ab.ToString();
}
}
catch (Exception ex)
{
throw (ex);
}
}
/// <summary>
/// Clear the Controlls
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReset_Click(object sender, EventArgs e)
{
try
{
txtNumber1.Text = string.Empty;
txtNumber2.Text = string.Empty;
txtNumber3.Text = string.Empty;
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
Addition, Subtraction and Division buttons has provided so that when user clicks on the Addition button then result will show Addition, when user clicks on the Substraction then result will show Substraction and same for Division.
I have also handled Error cases like Input Number should not be Empty and in case of Division, second number should be greater than 0 to avoid the Divide By 0 Exception
1. Addition
2. Substraction
3. Division