In C#, the Math class provides the constants and the static methods for logarithmic, trigonometric, and other mathematical functions. Math class has two fields as follows:
- Math.E Field
- Math.PI Field
Math.E Field
This field represents the natural logarithmic base, specified by the constant, e.
Syntax:
public const double E
Program 1: To illustrate the Math.E field in Math class
C#
using System;
class GFG {
static void Main()
{
double e = Math.E;
Console.WriteLine( "Math.E = " + e);
}
}
|
Output:
Math.E = 2.71828182845905
Program 2: Let’s see an example to compare Math.E with the value calculated from a power series.
C#
using System;
class GFG {
public static void Main()
{
double fact = 1.0;
double PS = 0.0;
for ( int n = 0; n < 10 &&
Math.Abs(Math.E - PS) > 1.0E-15; n++)
{
if (n > 0)
fact *= ( double )n;
PS += 1.0 / fact;
Console.WriteLine(PS);
Console.WriteLine(Math.E - PS);
}
}
}
|
Output:
1
1.71828182845905
2
0.718281828459045
2.5
0.218281828459045
2.66666666666667
0.0516151617923786
2.70833333333333
0.00994849512571205
2.71666666666667
0.00161516179237875
2.71805555555556
0.000226272903489644
2.71825396825397
2.7860205076724E-05
2.71827876984127
3.05861777505356E-06
2.71828152557319
3.02885852843104E-07
Math.PI Field
It represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI(Ï€).
Syntax:
public const double PI
Program 1: To illustrate the Math.PI field in Math class
C#
using System;
class GFG
{
static void Main()
{
double pi_value = Math.PI;
Console.WriteLine( "Math.PI = " + pi_value);
}
}
|
Output:
Math.PI = 3.14159265358979
Program 2: To demonstrate the multiplication of different data types with Math.PI constant value.
C#
using System;
class GFG
{
static void Main()
{
Console.WriteLine(1 * Math.PI );
Console.WriteLine(2 * Math.PI );
Console.WriteLine(1.5 * Math.PI );
Console.WriteLine(-3.10 * Math.PI );
}
}
|
Output:
3.14159265358979
6.28318530717959
4.71238898038469
-9.73893722612836
Program 3: Let’s Demonstrate the access of PI and find the Volume of the cylinder.
C#
using System;
class GFG {
static void Main()
{
double radius = 6;
double length = 9;
double area = radius * radius * Math.PI;
double volume = area * length;
Console.WriteLine( "Area of cylinder is : " + area);
Console.WriteLine( "Volume of cylinder is : " + volume);
}
}
|
Output:
Area of cylinder is : 113.097335529233
Volume of cylinder is : 1017.87601976309
References:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Jan, 2022
Like Article
Save Article