Вывести на экран число ПИ с точностью до сотых

thmn8

Администратор
Сообщения
1 472
Реакции
311
Сайт
tehadm.ru
C#:
static void Main(string[] args)
        {
            double pi = Math.PI;
            Console.WriteLine(pi);
            Console.ReadKey();
        }
 
А вот очень интересный пример реализации алгоритма расчета числа ПИ:
C#:
using System;

class Program
{
    static void Main()
    {
        // Get PI from methods shown here
        double d = PI();
        Console.WriteLine("{0:N20}",
            d);

        // Get PI from the .NET Math class constant
        double d2 = Math.PI;
        Console.WriteLine("{0:N20}",
            d2);
    }

    static double PI()
    {
        // Returns PI
        return 2 * F(1);
    }

    static double F(int i)
    {
        // Receives the call number
        if (i > 60)
        {
            // Stop after 60 calls
            return i;
        }
        else
        {
            // Return the running total with the new fraction added
            return 1 + (i / (1 + (2.0 * i))) * F(i + 1);
        }
    }
}
 
Назад
Верх Низ