-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (50 loc) · 1.69 KB
/
Program.cs
File metadata and controls
60 lines (50 loc) · 1.69 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
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
/*
Created by Kurt - LordTurtle419
Date: 18 October 2015
Problem - Find Pi to the Nth Digit, within a set limit
Solution - Determine the value of Pi,
Get Input from user to determine how many digits to display, limit is 14 decimals.
Validate input data
Display appropriate value.
*/
namespace FindPi
{
class Program
{
static void Main(string[] args)
{
const double Pi = Math.PI;
string value;
//Get user input to determine decimals to display, ensure data is valid before proceeding.
do
{
Console.WriteLine("Find Pi to the Nth Digit");
Console.WriteLine("Enter value for how many decimals to display - Limit is 14");
value = Console.ReadLine();
} while (IsDigitsOnly(value) == false);
//If the string value is greater than the limit (14), set the value in Math.Round to 14.
//Otherwise Convert string value to integer as normal
if (Convert.ToInt16(value) >= 15)
{
Console.WriteLine(Math.Round(Pi, 14));
Console.ReadKey(true);
}
else
{
Console.WriteLine(Math.Round(Pi, Convert.ToInt16(value)));
Console.ReadKey(true);
}
}
//Checks to see if value from user is numerical.
public static bool IsDigitsOnly(string value)
{
foreach (char c in value)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
}
}