Math Class Fields with Examples in C#


The Math class in C# has Math.E and Math.PI fields. Let us see an example of both the fields −

Math.E

Syntax

It is the natural logarithmic base specified by the constant e. The syntax is as follows −

public const double E = 2.71828182845905;

Example

Let us now see an example −

using System;
public class Demo{
   public static void Main(){
      double d = Math.E;
      Console.WriteLine("Math.E = " + d);
   }
}

Output

This will produce the following output −

Math.E = 2.71828182845905

Math.PI

The Math.PI field represents the ratio of the circumference of a circle to its diameter, specified by the constant, π.

Syntax

The syntax is as follows −

public const double PI = 3.14159265358979;

Example

Let us now see an example −

using System;
public class Demo{
   public static void Main(){
      double d = Math.PI;
      Console.WriteLine("Math.PI = " + d);
   }
}

Output

This will produce the following output −

Math.PI = 3.14159265358979
raja
Published on 08-Nov-2019 06:52:17
Advertisements