C# Math

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.

Common Math Methods in C#

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)

Example


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));
    

Math Constants

  • Math.PI โ€“ Value of ฯ€ (pi)
  • Math.E โ€“ Eulerโ€™s number

double radius = 5;
double area = Math.PI * Math.Pow(radius, 2);

Console.WriteLine(area);
    

Key Points

  • Math class provides built-in mathematical functions
  • No object creation is required (static methods)
  • Used for calculations and data processing
  • Improves code readability and accuracy

Conclusion

The Math class makes complex mathematical operations simple and efficient in C#. Understanding its methods helps in building accurate and high-performance applications.

C# Math Quiz

Q1. Which method returns square root?



Q2. Which constant stores value of pi?



Q3. Which method rounds down a value?


C# Introduction C# Variables & Keywords C# Data Types C# If Else Statement C# Loops C# Comments C# Type Casting C# User Input C# Operators C# Math C# String C# OOP Concepts C# Classes and Objects C# Multiple Classes and Objects C# Class Members C# Constructors C# Access Modifiers