In C#, the Math class provides methods to perform common mathematical operations such as rounding, power calculations, square roots, and more.
The Math class is part of the System namespace and is widely used in numerical and scientific calculations.
| Method | Description | Example |
|---|---|---|
| Math.Abs() | Returns absolute value | Math.Abs(-10) |
| Math.Sqrt() | Returns square root | Math.Sqrt(25) |
| Math.Pow() | Returns power of a number | Math.Pow(2, 3) |
| Math.Round() | Rounds a number | Math.Round(4.6) |
| Math.Floor() | Rounds down | Math.Floor(4.9) |
| Math.Ceiling() | Rounds up | Math.Ceiling(4.1) |
| Math.Max() | Returns larger value | Math.Max(10, 20) |
| Math.Min() | Returns smaller value | Math.Min(10, 20) |
int a = -9;
double b = 4.7;
Console.WriteLine(Math.Abs(a));
Console.WriteLine(Math.Sqrt(16));
Console.WriteLine(Math.Round(b));
Console.WriteLine(Math.Pow(2, 5));
double radius = 5;
double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine(area);
The Math class makes complex mathematical operations simple and efficient in C#. Understanding its methods helps in building accurate and high-performance applications.