Math Functions in C#
Math Functions in C#
Details: Math Functions in C#
Programming in a computer language is essential for creating any type of application. Most programming languages support programming elements such as conditional statements, loops and variables. Another critical element of any application is the use of math operators, functions and constants in the language. By choosing a language that supports math functions, you can easily develop powerful applications.
In this post, we will look into how to make a program that has a math function in C#. Here you will find out about Abs, Log, Round, Sin, Cos, and Tan functions.
Guide: Math Functions in C#
1. We start by creating a Windows Form Application in C# using the following steps in Microsoft Visual Studio 2010:
– Navigate to File.
– Click on New Project
– Select Windows Application.
2. Include 2 textboxes named txtNum for inputted numbers and txtOut for outputs.
Insert 6 Buttons;
btnAbs for Absolute Value
btnLog for Logarithm
btnRound for Rounding off
btnSin for finding the sine of an angle
btnCos for the Cosine of an
btnTan for the Tangent of the angle.
The interface can be designed as in the image below.

3. You can use the code below for the btnAbs. The Abs function returns the absolute value of the inputter number, that is, inputting a negative number will also give a positive output.
public void btnAbs_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Abs(Conversion.Val(txtNum.Text)));
}

4. The Log function will return the logarithmic value of the inputted number. Use the code below; btnLog for finding the Logarithm of a number.
public void btnLog_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Log(Conversion.Val(txtNum.Text)));
}

5. The btnRound function returns the inputted number rounding off to the nearest specified value. This simply means that if the nearest value after the decimal is 5, the whole number increases by 1. Use the code below for this function.
public void btnRound_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Round(Conversion.Val(txtNum.Text)));
}

6. Copy and paste the code below using btnSin for finding the sine value of the inputted number (angle). The Sin function will return the specific sine of an angle as our inputted number.
public void btnSin_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Sin(Conversion.Val(txtNum.Text)));
}

7. Using btnCos for finding the cosine value of the inputted number (angle), the code below can be utilized. This Cosine function will return the exact cosine of an angle as the inputted number.
public void btnCos_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Cos(Conversion.Val(txtNum.Text)));
}

8. The btnTan for finding the tangent value of the inputted number (angle), can be done using the code below. This Tan function will return the specifying tangent of an angle as our inputted number.
public void btnTan_Click(System.Object sender, System.EventArgs e)
{
txtOut.Text = System.Convert.ToString(Math.Tan(Conversion.Val(txtNum.Text)));
}
