Math Class

Learn how to perform common mathematical operations and calculations using the built-in static Math class.

We often need to perform mathematical calculations inside our programs. .NET provides the necessary tools out of the box.

The Math class

The System namespace contains a static class called Math with methods for performing various mathematical operations. For instance, we can calculate the square root of a number by calling the Math.Sqrt() method.

Note: The Math class is static, so all its members are also static. We use the class’s methods without instantiating it.

The Math class includes many methods. Here are some common examples:

  • Math.Pow(): This raises a number to a given power.

  • Math.Sqrt(): This finds the square root of a number.

  • Math.Cos(): This calculates the cosine of a given angle.

  • Math.Abs(): This returns the absolute value of a number.

  • Math.Cbrt(): This calculates the cubic root of a number.

Examples

Let’s create some useful programs as examples.

Calculate the area of a circle

Here’s the formula for the area of a circle: