Math class in C#
In C#, the Math class is a part of the .NET Framework’s standard library and provides a wide range of mathematical methods and constants for performing common mathematical operations. It is a static class, which means you don’t need to create an instance of it; you can access its methods and properties directly.
Key Features of the Math Class:
1. Static Methods: The Math class comprises static methods for executing mathematical operations like trigonometric functions, exponentiation, logarithms, and more.
2. Mathematical Constants: It provides essential mathematical constants such as π (pi) and the base of the natural logarithm.
Common Methods of the Math Class:
Rounding and Absolute Value: Utilize methods like Math.Round(), Math.Floor(), and Math.Abs() for rounding numbers and obtaining absolute values.
Math.Round() is used to round a numeric value to the nearest integer.
Math.Floor() is used to round a numeric value down to the nearest integer less than or equal to the given value.
Math.Abs() is used to obtain the absolute value of a numeric expression, which is its positive magnitude.
Square Root: Calculate square roots using Math.Sqrt().
Min and Max Values: Find the minimum and maximum of two values using Math.Min() and Math.Max().
The Math.Truncate() method in C# is used to remove the decimal part of a number, effectively rounding toward zero. It returns an integral part of the specified number as a double.
The Math.Pow() method in C# is used to calculate a specified number raised to the power of another specified number.
Examples of Common Methods:
// Math.Floor(): Rounds a number down to the nearest integer.
double weight = 10.75;
double floorWeight = Math.Floor(weight);
Console.WriteLine(floorWeight); // floorWeight is 10
// Math.Max(): Returns the larger of two numbers let’s take a set of 2 numbers (40, 60).
int greatermarks = Math.Max(40,60) // check the greater one
Console.WriteLine(greatermarks); // greatearmarks is 60
// Math.Min(): Returns the smaller of two numbers let’s take a set of 2 numbers (40, 60).
int smallermarks = Math.Min(40,60) // check the smaller one
Console.WriteLine(smallermarks); // smallermarks is 40
//Math.Sqrt(): Calculates the square root of a number.
double Area = 64;
double SqArea = Math.Sqrt(Area); // 8 x 8 = 64
Console.WriteLine(SqArea); // SqArea is 8
//Math.Abs(): Returns the absolute (non-negative) value of a number.
double temperature = Math.Abs(-5.2); // it converts negative value to positive
Console.WriteLine(temperature); //temperature is 5.2
//Math.Round(): Rounds a number to the nearest integer.
double height = Math.Round(7.6); // if height is less than 7.5 answer will be 7 otherwise height will be 8
Console.WriteLine(height); //rounded number is 8
//Math.Pow(): Raises a number to a specified power.
double result = Math.Pow(2.0, 3.0); // double result = 2 x 2 x 2 = 8;
Console.WriteLine(result); //result is 8
//Math.Truncate(): Removes the decimal part of a number (rounds to zero).
double amount = Math.Truncate(100000.897);
Console.WriteLine(amount); //amount is 100000
Course Video
Task:
1. Write a program to round the following number to the nearest integer using Math.Round
2. Write a program to round the following number down to the nearest integer using Math.Floor:
3. Write a program to get the absolute value of the following number using Math. Abs.
4. Write a program to calculate the square root of the following number using Math.Sqrt.
5. Write a program to find the maximum of the following two numbers using Math.Max.
6. Write a program to find the minimum of the following two numbers using Math.Min.
7. Write a program to remove the decimal part of the following number using Math.Truncate.
8. Write a program to calculate 3 raised to the power of 4 using Math.Pow.
9. Write a program that performs the following operations:
Rounds the number 5.8 to the nearest integer.
Finds the absolute value of -10.
Calculates the square root of 144.
Finds the maximum between 10 and 20.
Finds the minimum between 5 and 15.
Removes the decimal part of 9876.54321.
Raises 2 to the power of 5.