Sure! In C#, the System.Math
class provides a wide range of mathematical functions for performing common mathematical operations. Here's a simple explanation of some of the functions along with examples:
Abs()
: Returns the absolute value of a number.Example
using System;
class Program
{
static void Main()
{
int number = -10;
int absoluteValue = Math.Abs(number);
Console.WriteLine("Absolute value of " + number + " is: " + absoluteValue);
}
}
Absolute value of -10 is: 10
Sqrt()
: Returns the square root of a number.Example
using System;
class Program
{
static void Main()
{
double number = 25;
double squareRoot = Math.Sqrt(number);
Console.WriteLine("Square root of " + number + " is: " + squareRoot);
}
}
Square root of 25 is: 5
Pow()
: Returns a specified number raised to the power of another number.Example
using System;
class Program
{
static void Main()
{
double baseNumber = 2;
double exponent = 3;
double result = Math.Pow(baseNumber, exponent);
Console.WriteLine(baseNumber + " raised to the power of " + exponent + " is: " + result);
}
}
2 raised to the power of 3 is: 8
Round()
: Rounds a decimal value to the nearest integer.
using System;
class Program
{
static void Main()
{
double number = 3.7;
double roundedValue = Math.Round(number);
Console.WriteLine("Rounded value of " + number + " is: " + roundedValue);
}
}
Rounded value of 3.7 is: 4
These are just a few examples of the mathematical functions provided by the System.Math
class in C#. It offers many more functions for performing various mathematical calculations.