---
title: "C# program for a calculator"  
description: "private void n1_Click(object sender, EventArgs e)     if (textBox1.Text == \"0\" && textBox1.Text != null)         textBox1.Text = \"1\";     else"  
author: "elpozo lee"  
published: 2019-09-27  
updated: 2019-09-27  
canonical: https://www.mindstick.com/blog/185528/c-sharp-program-for-a-calculator  
category: "c#"  
tags: ["programming language", "programming homework"]  
reading_time: 5 minutes  

---

# C# program for a calculator

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void n1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "1";

}

else

{

textBox1.Text = textBox1.Text + "1";

}

}

private void n1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "1";

}

else

{

textBox1.Text = textBox1.Text + "1";

}

}

private void n2_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "2";

}

else

{

textBox1.Text = textBox1.Text + "2";

}

}

private void n3_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "3";

}

else

{

textBox1.Text = textBox1.Text + "3";

}

}

private void n4_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "4";

}

else

{

textBox1.Text = textBox1.Text + "4";

}

}

private void n5_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "5";

}

else

{

textBox1.Text = textBox1.Text + "5";

}

}

private void n6_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "6";

}

else

{

textBox1.Text = textBox1.Text + "6";

}

}

private void n7_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "7";

}

else

{

textBox1.Text = textBox1.Text + "7";

}

}

private void n8_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "8";

}

else

{

textBox1.Text = textBox1.Text + "8";

}

}

private void n9_Click(object sender, EventArgs e)

{

if (textBox1.Text == "" && textBox1.Text != null)

{

textBox1.Text = "9";

}

else

{

textBox1.Text = textBox1.Text + "9";

}

}

private void n0_Click(object sender, EventArgs e)

{

textBox1.Text = textBox1.Text + "0";

}

using [System](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business);

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

[namespace](https://www.mindstick.com/articles/12552/namespace) CalculatorApp

{

public [partial class](https://www.mindstick.com/forum/155717/define-partial-class-in-mvc) Form1 : Form

{

double FirstNumber;

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) [Operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp);

public Form1()

{

InitializeComponent();

}

private void n1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "1";

}

else

{

textBox1.Text = textBox1.Text + "1";

}

}

private void n2_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "2";

}

else

{

textBox1.Text = textBox1.Text + "2";

}

}

private void n3_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "3";

}

else

{

textBox1.Text = textBox1.Text + "3";

}

}

private void n4_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "4";

}

else

{

textBox1.Text = textBox1.Text + "4";

}

}

private void n5_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "5";

}

else

{

textBox1.Text = textBox1.Text + "5";

}

}

private void n6_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "6";

}

else

{

textBox1.Text = textBox1.Text + "6";

}

}

private void n7_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "7";

}

else

{

textBox1.Text = textBox1.Text + "7";

}

}

private void n8_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text != null)

{

textBox1.Text = "8";

}

else

{

textBox1.Text = textBox1.Text + "8";

}

}

private void n9_Click(object sender, EventArgs e)

{

if (textBox1.Text == "" && textBox1.Text != null)

{

textBox1.Text = "9";

}

else

{

textBox1.Text = textBox1.Text + "9";

}

}

private void n0_Click(object sender, EventArgs e)

{

textBox1.Text = textBox1.Text + "0";

}

private void bad_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = "0";

Operation = "+";

}

private void bsub_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = "0";

Operation = "-";

}

private void bmul_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = "0";

Operation = "*";

}

private void bdiv_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = "0";

Operation = "/";

}

private void bc_Click(object sender, EventArgs e)

{

textBox1.Text = "0";

}

private void ndot_Click(object sender, EventArgs e)

{

textBox1.Text = textBox1.Text + ".";

}

private void nequal_Click(object sender, EventArgs e)

{

double SecondNumber;

double [Result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency);

SecondNumber = Convert.ToDouble(textBox1.Text);

if (Operation == "+")

{

Result = (FirstNumber + SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "-")

{

Result = (FirstNumber - SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "*")

{

Result = (FirstNumber * SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "/")

{

if (SecondNumber == 0)

{

textBox1.Text = "Cannot [divide by zero](https://www.mindstick.com/forum/158622/describe-the-concept-of-a-divide-by-zero-exception-and-how-to-handle-it)";

}

else

{

Result = (FirstNumber / SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

}

}

}

}

Try this code by yourself .... happy #coding cheere :)

## Sarkari Result Com

---

Original Source: https://www.mindstick.com/blog/185528/c-sharp-program-for-a-calculator

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
