c sharp

How to Use Mathematical Functions in C#

C# provides a wide range of mathematical functions that are essential for various applications ranging from scientific computing to game development. The mathematical functions in C# are included in the System.Math namespace can be used for performing arithmetic operations, rounding numbers, calculating trigonometric functions, and much more.

In this tutorial, we will see the different types of mathematical functions available in C# and how to properly use them in C# code.

How to Use Mathematical Functions in C#

Mathematical functions in C# are an integral part of the .NET framework and are included in the System. Math namespace. Some of the commonly used functions are discussed in the next section of this guide.

1: Math.Max(a,b)

One of the many useful functions provided by Math class is Max, which can be used to determine the highest value between two operands. The following example program demonstrates how to compare these two values using the Max function and obtain the maximum number as a result.
For example:

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine (Math.Max(200,10));
    }
}

The above program sets the values of a and b to 10 and 200 respectively, then uses Math.Max to compare them and store the result in the variable max. The program then outputs the result to the console through Console.WriteLine, which prints the maximum value between a and b.

Output

2: Math.Min(a,b)

The Math class in C# also provides a Min function, which can be used to determine the minimum value between two operands, a and b. In the below example code, we pass 200, 10 as the first and second argument to the Math.Min function, which then compares the two values and returns an integer representing the smallest value:

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.Min(200,10));
    }
}

Output

3: Math.Sqrt(a)

In C#, the Math.Sqrt function computes the square root of a given number. This function only accepts positive numbers as input.

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine (Math.Sqrt(100));
    }
}

The above code uses the Math.Sqrt() function finds the square root of the entered number 100 and prints the result to the console using Console.WriteLine(). The using statement at the beginning of the code imports the System namespace, which contains the Math class used in the code.

Output

4: Math.Abs(a)

To get the absolute value of the input number in C# use Math.Abs method:

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.Abs(-22));
    }
}

The code above gives the absolute value of -22, and then outputs the result to the console using the Console.WriteLine method.

Output

5: Math.Round(a)

The Math.Round method in C# is commonly used in mathematical calculations involving trigonometry and statistics. This method rounds the entered number to the nearest whole number.

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.Round(22.678));
    }
}

The above code uses the Math.Round function from the System.Math namespace to round a given decimal number to the nearest whole number.

Output

7: Math.Truncate(a)

The Math.Truncate function in C# is used to remove the decimal or fractional part of a given number and return the resulting integer. This function accepts either the decimal or double data type as input.

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.Truncate(22.678));
    }
}

The above program uses the Math.Truncate function in C# to remove the decimal or fractional part of a given number.

Output

8: Math.Pow

In C#, the Math.Pow method is used to calculate the power of a given base value. This method takes two arguments: the first argument is the base value, and the second argument is the power value.

using System;

public class calculations
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Math.Pow(2,3));
    }
}

The above code uses Math.Pow function in C# to find the output of 2 raised to the power of 3.

Output

9: Trigonometric Functions

The Math class in C# also provides several methods for trigonometric calculations, including Math.Sin(), Math.Cos(), and Math.Tan(). These methods take a double data type value as input and return the same value. However, before passing the input value to these functions, it needs to be converted to radians.

using System;

public class Sin {
  public static void Main(string[] args) {
    double sinAngle = (90 * (Math.PI)) / 180;
    Console.WriteLine(Math.Sin(sinAngle));
  }
}

The above code calculates the sine value of an angle in degrees (90) by first converting it to radians using the mathematical constant PI (π) and then using the Math.Sin() function to compute the sine of the angle.

Output

10: Math.Floor(a)

The Math.Floor method in C# displays the output of the entered number to the greatest or less than or equal to the entered number. This method can be used to truncate decimal values, effectively rounding them down to the nearest whole number.

using System;

class floor
{
    static void Main(string[] args)
    {
        double number = 5.8;
        double result = Math.Floor(number);
        Console.WriteLine(result);  
    }
}

The above code uses Math.Floor method in C# to round a given number down to the closest integer number.

Output

Bottom Line

C# provides various mathematical functions that are included in the System.Math namespace. They can be used for performing different operations. The article covers some of the commonly used functions in C#, including Math.Max(), Math.Min(), Math.Sqrt(), Math.Abs(), Math.Round(), Math.Truncate(), Math.Pow(), and trigonometric functions such as Math.Sin(), Math.Cos(), and Math.Tan(). These functions are an integral part of the .NET framework and can be used to perform complex mathematical calculations with ease.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.