Double Precision in C#

  • Thread starter Thread starter Arash_89
  • Start date Start date
A

Arash_89

Guest
Hello,

I wrote a program for calculate the below series.

I calculate this manually and we have 0.34 for n = 2

But in program output is 0.333

How can I fix this?

Thanks

1580492.jpg


using System;
using System.Transactions;

namespace ConsoleApplication5
{
class program
{
static double Cal(int n)
{
double Sum = 1;
for (int i = 1; i <= n; i++)
{
Sum *= 1.0 / (i + 1);
}
return Sum;
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
double S = 0;
int Sign = -1;
for (int i = 1; i <= n; i++)
{
Sign *= -1;
double T = Sign * Cal(i);
S += T;
}
Console.WriteLine("{0:F3}",S);
}
}
}

Continue reading...
 
Back
Top