C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Math Class

In this tutorial, we will explore the Math class in C#, which provides methods for performing various mathematical operations. The Math class is part of the System namespace and offers a collection of static methods to work with numeric values.

  • Basic Usage

To use the Math class, you do not need to create an instance of it, as all its methods are static. Simply call the method you need using the Math class name:

double result = Math.MethodName(arguments);
  • Common Math Methods

Here are some commonly used methods in the Math class:

  • Abs: Returns the absolute value of a number.
int absValue = Math.Abs(-5); // Output: 5
  • Ceiling: Returns the smallest integer greater than or equal to the specified number.
double ceiling = Math.Ceiling(3.4); // Output: 4
  • Floor: Returns the largest integer less than or equal to the specified number.
double floor = Math.Floor(3.4); // Output: 3
  • Round: Rounds a number to the nearest integer, or to the specified number of fractional digits.
double rounded = Math.Round(3.5); // Output: 4
  • Max: Returns the larger of two numbers.
int max = Math.Max(3, 5); // Output: 5
  • Min: Returns the smaller of two numbers.
int min = Math.Min(3, 5); // Output: 3
  • Pow: Raises a number to the specified power.
double pow = Math.Pow(2, 3); // Output: 8
  • Sqrt: Returns the square root of a number.
double sqrt = Math.Sqrt(9); // Output: 3
  • Sin, Cos, and Tan: Calculate the sine, cosine, and tangent of an angle, respectively. The angle must be in radians.
double angleInRadians = Math.PI / 4; // 45 degrees in radians
double sinValue = Math.Sin(angleInRadians);
double cosValue = Math.Cos(angleInRadians);
double tanValue = Math.Tan(angleInRadians);
  • Asin, Acos, and Atan: Calculate the inverse sine, cosine, and tangent of a value, respectively. The result is in radians.
double value = 0.5;
double angleInRadians = Math.Asin(value);
double angleInDegrees = angleInRadians * (180 / Math.PI); // Convert radians to degrees
  • Constants

The Math class also provides two commonly used mathematical constants:

  • Math.PI: Represents the value of �� (approximately 3.14159).
  • Math.E: Represents the value of e (approximately 2.71828).

For example:

double circumference = 2 * Math.PI * 5; // Calculate the circumference of a circle with radius 5

This tutorial introduces the Math class in C#, which provides various static methods for performing mathematical operations on numeric values. These methods are essential when working with numbers in your applications, and the Math class offers a convenient way to perform these operations without implementing them from scratch.

  1. How to use Math class in C#

    The Math class in C# provides a set of static methods for performing mathematical operations. Here's a basic example:

    using System;
    
    class Program
    {
        static void Main()
        {
            double result = Math.Sqrt(25); // Square root
            Console.WriteLine($"Square root of 25: {result}");
    
            Console.ReadLine();
        }
    }
    
  2. C# Math operations examples

    double x = 10.5;
    double y = 3.2;
    
    double sum = Math.Add(x, y);         // Addition
    double difference = Math.Subtract(x, y);  // Subtraction
    double product = Math.Multiply(x, y);    // Multiplication
    double quotient = Math.Divide(x, y);     // Division
    
  3. Working with constants in Math class in C#

    The Math class provides constants like Pi and E:

    double circleArea = Math.PI * Math.Pow(radius, 2);
    
  4. Trigonometric functions in Math class in C#

    double angle = 45; // in degrees
    double radians = Math.PI * angle / 180;
    
    double sineValue = Math.Sin(radians);
    double cosineValue = Math.Cos(radians);
    double tangentValue = Math.Tan(radians);
    
  5. C# Math class rounding methods

    double number = 3.75;
    
    double roundedUp = Math.Ceiling(number);    // Rounds up to the nearest integer (4.0)
    double roundedDown = Math.Floor(number);     // Rounds down to the nearest integer (3.0)
    double rounded = Math.Round(number);         // Rounds to the nearest integer (4.0)
    
  6. Using Math.Pow in C#

    double baseNumber = 2;
    double exponent = 3;
    
    double result = Math.Pow(baseNumber, exponent); // 2^3 = 8
    
  7. C# Math class for random numbers

    Random random = new Random();
    int randomNumber = random.Next(1, 101); // Generates a random number between 1 and 100
    
  8. C# Math class and logarithmic functions

    double number = 100;
    
    double logBase10 = Math.Log10(number);      // Logarithm base 10
    double naturalLog = Math.Log(number);       // Natural logarithm (base e)