Hi! I tried to write the following program in C# to calculate pi using infinite recursion, but I keep getting confused about integer/double/decimal division.
I really have no clue why this isn't working, so pardon me for my lack of understanding of strongly typed stuff, as I'm still learning C#.
Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static int Main(string[] args)
{
int numeratornext = 2;
int denominatornext = 5;
decimal findto = 100.0M;
decimal pi = 0.0M;
decimal halfpi = 1.0M;
int seriesnum = 1;
int seriesden = 3;
for (int i = 0; i < findto; i++)
{
halfpi += Decimal.Divide((decimal)seriesnum, (decimal)seriesden);
//System.Console.WriteLine(Decimal.Divide((decimal)seriesnum, (decimal)seriesden).ToString());
seriesnum *= numeratornext;
seriesden *= denominatornext;
numeratornext++;
denominatornext += 2;
}
pi = halfpi * 2;
System.Console.WriteLine(pi.ToString());
System.Console.ReadLine();
return 0;
}
}
}