-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (43 loc) · 1.78 KB
/
Copy pathProgram.cs
File metadata and controls
46 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FindPIRandomNumbers
{
class Program
{
static void Main(string[] args)
{
double pi; // Stores the value of Pi
double inside=0; // stores the number of points inside the circle
double total=0; // stores totol number of points generated
double points;//stores the number of points to generate
Random random = new Random();
Console.Write("This program will attempt to find the value of Pi using random numbers.\nThe higher the number of points, the more accurate the value of Pi will be.\nPlease input the number of points to generate:");
while (!double.TryParse(Console.ReadLine(), out points) || points%1!=0) //Retrieves the number of points to generate and checks for int only
{
Console.Clear();
Console.Write("You have inserted an invalid number.\nPlease input the number of points to generate:");
}
for(double i = 0; i<points; i++) // generates each point
{
double x = random.NextDouble(); // generates random double between 0 and 1
double y = random.NextDouble(); // generates random double between 0 and 1
double z = Math.Sqrt((y * y) + (x * x));
if (z <= 1) //checks if it is in the circle
{
inside++;
total++;
}
else
{
total++;
}
}
pi = 4*(inside / total);
Console.Write(pi);
Console.ReadKey();
}
}
}