EN
C# / .NET - Math.PI property example
0 points
The Math.PI
property returns π number (3.141592653589793...
).
xxxxxxxxxx
1
using System;
2
3
class Program
4
{
5
static void Main(string[] args)
6
{
7
// ------------------------------------------------------
8
// Math.PI value priting:
9
10
Console.WriteLine(Math.PI); // 3.141592653589793
11
12
// ------------------------------------------------------
13
// Math.PI with circle:
14
15
double radius = 5;
16
17
// 1. Circle surface area:
18
double area = Math.PI * Math.Pow(radius, 2);
19
Console.WriteLine(area); // 78.53981633974483
20
21
// 2. Circle circumference:
22
double circumference = 2 * Math.PI * radius;
23
Console.WriteLine(circumference); // 31.41592653589793
24
}
25
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public const double PI = 3.1415926535897931; 7 // ... 8 } 9 } |
Result | π number (3.141592653589793... ). |
Description |
|
To calculate PI number Nilakantha series can be used.

xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double ComputePi(int iterations)
6
{
7
double approximation = 3;
8
9
double a = 2;
10
for (int i = 0; i < iterations; i++)
11
{
12
approximation += 4 / (a * (++a) * (++a));
13
approximation -= 4 / (a * (++a) * (++a));
14
}
15
return approximation;
16
}
17
18
public static void Main(string[] args)
19
{
20
Console.WriteLine( ComputePi( 1 )); // 3.1333333333333333
21
Console.WriteLine( ComputePi( 2 )); // 3.1396825396825396
22
Console.WriteLine( ComputePi( 5 )); // 3.1414067184965018
23
Console.WriteLine( ComputePi( 10 )); // 3.141565734658547
24
Console.WriteLine( ComputePi( 20 )); // 3.141589028940776
25
Console.WriteLine( ComputePi( 50 )); // 3.1415924109719824
26
Console.WriteLine( ComputePi( 100 )); // 3.141592622804848
27
Console.WriteLine( ComputePi( 200 )); // 3.1415926497127264
28
Console.WriteLine( ComputePi( 500 )); // 3.141592653340544
29
Console.WriteLine( ComputePi( 1000 )); // 3.141592653558594
30
Console.WriteLine( ComputePi( 2000 )); // 3.141592653585895
31
Console.WriteLine( ComputePi( 5000 )); // 3.141592653589538
32
}
33
}