Loading lessons...
C# Math
C# Math
C# has a built-in Math class full of methods for common math tasks.
Math.Max and Math.Min
Math.Max(x, y) returns the largest value; Math.Min(x, y) returns the smallest:
Console.WriteLine(Math.Max(5, 10)); // 10
Console.WriteLine(Math.Min(5, 10)); // 5
Math.Sqrt
Math.Sqrt(x) returns the square root:
Console.WriteLine(Math.Sqrt(64)); // 8
Math.Abs
Math.Abs(x) returns the absolute value (always positive):
Console.WriteLine(Math.Abs(-4.7)); // 4.7
Math.Round
Math.Round(x) rounds to the nearest whole number:
Console.WriteLine(Math.Round(9.99)); // 10
Console.WriteLine(Math.Round(4.4)); // 4
Math.Pow and more
Math.Pow(x, y)- raises x to the power y.Math.Cos,Math.Sin,Math.Tan- trigonometry.Math.PI- the constant 3.14159...
Console.WriteLine(Math.Pow(2, 10)); // 1024
Console.WriteLine(Math.PI);
TL;DR
Math.Max,Math.Min- largest / smallest.Math.Sqrt,Math.Abs,Math.Round,Math.Pow.Math.PIgives the constant pi.