C# Math

C# Math

C# provides a Math class in the System namespace that provides a wide range of mathematical functions. Here are some of the most commonly used methods in the Math class:

  1. Abs(): Returns the absolute value of a number.
				
					int a = -10;
int b = Math.Abs(a); // b = 10	

				
			
  1. Round(): Rounds a number to the nearest integer.
				
					double a = 3.14159;
double b = Math.Round(a); // b = 3

				
			
  1. Floor(): Returns the largest integer less than or equal to a given number.
				
					double a = 3.9;
double b = Math.Floor(a); // b = 3

				
			
  1. Ceiling(): Returns the smallest integer greater than or equal to a given number.
				
					double a = 3.1;
double b = Math.Ceiling(a); // b = 4

				
			
  1. Sqrt(): Returns the square root of a number.
				
					double a = 16;
double b = Math.Sqrt(a); // b = 4

				
			
  1. Pow(): Returns the result of raising a number to a specified power.
				
					double a = 2;
double b = Math.Pow(a, 3); // b = 8

				
			
  1. Max(): Returns the larger of two numbers.
				
					int a = 10;
int b = 20;
int c = Math.Max(a, b); // c = 20

				
			
  1. Min(): Returns the smaller of two numbers.
				
					int a = 10;
int b = 20;
int c = Math.Min(a, b); // c = 10

				
			

These are just a few of the many functions provided by the Math class in C#. The exact function you need will depend on the specific program you’re writing.

Join To Get Our Newsletter
Spread the love