Simple Scientific Calculator Using C# In Visual Studio


In this article, you will see how we create a Scientific calculator using C#.


C# is an object-oriented programming language, developed by Microsoft in 2000 to adopt the best features of Java and C++. It is used for a wide range of reasons, but its popularity lies in its use for the following tasks.
  1. Back End services
  2. Game Development
  3. Windows Application
  4.  Web Development

Features of C#

  • Type-Safe C# type safe code can only access the memory location that it has permission to execute. Therefore it improves the security of the program.
  • Simple C# is a simple language in the sense that it provides a structured approach (to break the problem into parts), rich set of library functions, data types etc.
  • Rich Libraries C# provides a lot of inbuilt functions that make the development fast.
  • Object-Oriented C# is an object-oriented programming language. OOPs makes development and maintenance easier whereas in Procedure-oriented programming language it is not easy to manage if code grows as project size grow.

What makes C# better?

  • It is a modern, general-purpose programming language
  • It is object-oriented.
  • It is component-oriented.
  • It is easy to learn.
  • It is a structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • It is a part of .Net Framework.

.NET Framework

The .Net framework is a revolutionary platform that helps you to write the following types of applications,
  • Windows applications
  • Web applications
  • Web services
The .Net framework applications are multi-platform applications. The framework has been designed in such a way that it can be used from any of the following languages C#, C++, Visual Basic, Jscript, COBOL, etc. All these languages can access the framework as well as communicate with each other.
The .Net framework consists of an enormous library of codes used by the client languages such as C#. Following are some of the components of the .Net framework
  • Common Language Runtime (CLR)
  • The .Net Framework Class Library
  • Common Language Specification
  • Common Type System
  • Metadata and Assemblies
  • Windows Forms
  • ASP.Net and ASP.Net AJAX
  • ADO.Net
  • Windows Workflow Foundation (WF)
  • Windows Presentation Foundation
  • Windows Communication Foundation (WCF)
  • LINQ

What is a Calculator and why is it important?

Calculators are simply a tool students use to help solve problems. Since they eliminate tedious computations and algebraic manipulations that discourage many students, calculators allow more students to solve problems and appreciate the power and value of mathematics in the world today.
Nowadays, calculators in institutions are just as widely made use of as computers are. Due to the fact that its development virtually forty years back, the electronic calculator has evolved from a device that might only perform simple four-function procedures into one that may now likewise implement extremely technological algebraic emblematic manipulations quickly as well as accurately.

What is a Scientific calculator and why is it important?

By definition, a scientific calculator is a calculator designed to help you calculate science, engineering, and mathematics problems. It has way more buttons than your standard calculator that just lets you do your four basic arithmetic operations of addition, subtraction, multiplication, and division.
All these extra buttons allow you to work with various kinds of numbers and problems such as these
  • Trigonometry problems
  • Scientific numbers that have a multiplication by 10 to a certain power.
  • Pi problems
  • Logarithm problems with base 10 and the natural base
  • Probability problems that use the factorial function
You can now use your calculator to help you solve trigonometry problems involving sine, cosine, tangent, their inverses, and their hyperbolic functions. When working with trigonometric values, you can change the calculations between degrees, radians, and grads. Also, you now have access to a button for pi and Euler's constant, e. There are also buttons that allow you to easily calculate exponents to the second, third, or any other power.
When working with scientific numbers, there is an Exp button that lets you easily and quickly input scientific numbers.
Engineering problems make use of exponents, logs, and scientific numbers.
Also, all of these types of problems are usually longer expressions that involve several steps to solve by hand. But with the use of a scientific calculator, you can now input the whole expression, push the equals button, and the calculator will perform all the calculations you need in the right order. Yes, the scientific calculator computes your problems following the order of operations.
Special Buttons
To help you write your equations, your scientific calculator has some special buttons.
For scientific numbers, you have a special button that automatically adds the multiplication by 10 to a certain exponent. This button is the Exp button. You input your scientific number, then push the button, and then input your exponent value.

Also, for working with engineering problems, you can calculate a number to any exponent and you can take a number and find the square root, the third root, and any other root by using these buttons.

And of course, you have your trigonometric buttons.

Implementation Using C#


Toolboxes Used

The Textbox
The text box is for accepting input from the user as well as to display the output. It can handle string and numeric data but not images or pictures. String in a text box can be converted to numeric data by using the function Val(text). The label control is only for displaying a caption/title or to display an output.
The Label
The label can be used to provide instructions and guides to the user as well as to display the output. It is different from TextBox because it can only display static text, which means the user cannot change the text. Using the syntax Label.Text can display text and numeric data. You can change its text in the properties window or program it to change at runtime.
The Button 
The Button control represents a standard Windows button. It is generally used to generate a Click event by providing a handler for the Click event.

Math Class in C#

In C#, Math class comes under the System namespace. It is used to provide static methods and constants for logarithmic, trigonometric, and other useful mathematical functions. It is a static class and inherits the object class.
Below is the list of Math functions used in this project 
  • Math.PI It represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI(π). 
  • Math.Cos() Returns the cosine of the specified angle.
  • Math.Sin() Returns the sine of the specified angle.
  • Math.Tan() Returns the tangent of the specified angle. 
  • Math.Cosh() Returns the hyperbolic cosine of the specified angle. 
  • Math.Sinh(): Returns the hyperbolic sine of the specified angle. 
  • Math.Tanh() Returns the hyperbolic tangent of the specified angle. 
  • Math.Exp() Returns e raised to the power of a specific number. 
  • Math.Floor() Returns the largest integral value less than or equal to the specified number. 
  • Math.Pow() Returns a specified number raised to the specified power.
  • Math.Sqrt() Returns the square root of the specified number.
  • Math.Log() Returns the logarithm of a specified number. 
  • Math.Log10() Returns the base 10 logarithms of a specified number. 
  • Math.Ceiling(): Return the smallest integral value greater than or equal to the specified number.

Code Implementation

Button 0-9
  1. private void button1_Click(object sender, EventArgs e) {  
  2.     if (textBox.Text == "0" || isoptr) {  
  3.         textBox.Clear();  
  4.     }  
  5.     isoptr = false;  
  6.     Button button = (Button) sender;  
  7.     if (textBox.Text == ".") {  
  8.         if (!textBox.Text.Contains(".")) {  
  9.             textBox.Text += button.Text;  
  10.         }  
  11.     } else textBox.Text += button.Text;  
  12.     //textBox.Text = button.Text;    
  13. }   
Buttons Operators 
  1. private void button39_Click(object sender, EventArgs e) {  
  2.     switch (abbb) {  
  3.         case "+"  
  4.         textBox.Text = (firstdigit + double.Parse(textBox.Text)).ToString();  
  5.         break;  
  6.         case "-"  
  7.         textBox.Text = (firstdigit - double.Parse(textBox.Text)).ToString();  
  8.         break;  
  9.         case "*"  
  10.         textBox.Text = (firstdigit * double.Parse(textBox.Text)).ToString();  
  11.         break;  
  12.         case "/"  
  13.         textBox.Text = (firstdigit / double.Parse(textBox.Text)).ToString();  
  14.         break;  
  15.     }  
  16. }   
Backspace button 
  1. private void button22_Click(object sender, EventArgs e) {  
  2.     int index = textBox.Text.Length;  
  3.     index--;  
  4.     textBox.Text = textBox.Text.Remove(index);  
  5.     if (textBox.Text == "") {  
  6.         textBox.Text = "0";  
  7.     }  
  8. }   
Clear button 
  1.  private void button21_Click(object sender, EventArgs e)  
  2.  {  
  3.     textBox.Text = "0";  
  4.  }  
± button
  1.  private void button23_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = result * -1;  
  5.     textBox.Text = result.ToString();  
  6.  }  
Square root button
  1.  private void button11_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Sqrt(result);  
  6.     textBox.Text = result.ToString();  
  7.  }  
% button
  1.  private void button18_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = result / 100;  
  5.     textBox.Text = result.ToString();  
  6.  }  
Ceil Button
  1.  private void button19_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Ceiling(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Floor Button
  1.  private void button20_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Floor(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Square Button
  1.  private void button12_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Pow(result,2);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Cube Button
  1.  private void button14_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Pow(result,3);  
  5.     textBox.Text = result.ToString();  
  6.  }  
1/x Button
  1.  private void button14_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = 1/result;  
  5.     textBox.Text = result.ToString();  
  6.  }  
MOD Button
  1.  private void button15_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = (firstdigit % double.Parse(textBox.Text));  
  5.     textBox.Text = result.ToString();  
  6.  }  
Log Button
  1.  private void button6_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Log10(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Sin Button
  1.  private void button7_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Sin(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Cos Button
  1.  private void button8_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Cos(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Tan Button
  1.  private void button9_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Tan(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
Pi Button
  1.  private void button10_Click(object sender, EventArgs e)  
  2.  {  
  4.     result = Math.PI;  
  5.     textBox.Text = result.ToString();  
  6.  }  
Exponential Button
  1.  private void button5_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Exp(result);  
  5.     textBox.Text = result.ToString();  
  6. }  
TanH Button
  1.  private void button4_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Tanh(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
CosH Button
  1.  private void button3_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Cosh(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
SinH Button
  1.  private void button2_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Sinh(result);  
  5.     textBox.Text = result.ToString();  
  6.  }  
OFF Button
  1.  private void button1_Click_1(object sender, EventArgs e)  
  2.  {  
  3.     Application.Exit();  
  4.  }  
Natural Log x button
  1.  private void button17_Click(object sender, EventArgs e)  
  2.  {  
  3.     result = double.Parse(textBox.Text);  
  4.     result = Math.Log(result);  
  5.     textBox.Text = result.ToString();}  
Here is the output in which I have performed basic multiplication and used the square root method.
Expected output:
Square Root
Expected Output


In this article, we learned how to make a Scientific calculator using C#.