Hi i am trying to code Calculate the first n digits of pi
using System;
using System.Numerics;
namespace PiCalculator
{
class Program
{
static void Main(string[] args)
{
System.Decimal.Precision = 5;
Console.WriteLine("How Many Digit?");
int n = Convert.ToInt32(Console.ReadLine());
decimal pi = 0;
decimal t = 0;
decimal deno = 0;
int k = 0;
t=1*(Factorial(1))*(13591409+545140134*k);
deno = ((decimal)Factorial(3*k))*((decimal)Math.Pow((double)Factorial(k),3))*((decimal)(Math.Pow(640320,(3*k))));
pi += t/deno;
pi = pi * 12 / (decimal)Math.Pow(640320,1.5);
pi = 1/pi;
Console.WriteLine(pi);
}
// Factorial fonksiyonu
static decimal Factorial(int n)
{
if (n == 0)
return 1;
return n * Factorial(n - 1);
}
}
}
I am new on C# i need help with set Decimal.Precision pls help.
I am trying to calculate n number of pi and write it.
Code can calculate but i dont know how can write n number of this calculation result.
You can write out the substring of pi from 0 to n:
replace: Console.WriteLine(pi);
with: Console.WriteLine(pi.ToString().Substring(0, (n > 1)? n + 1: n));
the n + 1 logic increases the number of characters since the decimal "." does not count as a digit. Notice to increase by 1 only if user specifies more than 1 digit (n > 1).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.