Evaluating Neper's Numbers in C#


One of the most interesting number in mathematics is the Neper's number : e. The mathematicians know very well its importance in calculus and analysis. This program in C#, is a simple way to evaluate the value of e. Its an irrational number, so it has infinite digits after the floating-point. In the arithmetic of calculator, we always work with rational numbers because we can only use a finite number of digits, so we will have an approximation. We will use, the constant field of class Math, Math.E that returns the double value of e, for evaluate the error of precision.  The program uses the mathematical property of succession An= (1+1/n)^n that converge to "e" for n->infinite. The program is really simple, so now, take a  look at the code.

using System;  
class Neper
{
public static void Main()
{
const double maxAcceptable = 2000000000;
int Percent = 0;
double Value, Error, Iteration;
Console.WriteLine("******************** NUMBER e ********************");
Console.Write("How many iteration? ");
Iteration = Double.Parse(Console.ReadLine());
Console.WriteLine("----");
Console.WriteLine("The value of Math.E is: " + Math.E);
Console.WriteLine("----");
if (Iteration < maxAcceptable)
{
for(double i=1; i<=Iteration; i++)
{
Value = Math.Pow((1 + 1/i),i);
if (i % (Iteration/100) == 0)
{
Percent = Percent + 1;
Console.WriteLine(Percent + "% of computation");
Error = (Value - Math.E);
Console.WriteLine("Partial: " + Value + " Error: " + Error);
Console.WriteLine("----");
}
}
}
else
{
Console.WriteLine("Do you have a long free time?!");
Console.WriteLine("Execution will stop!");
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all