Computing nine factorial with LINQ - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Computing nine factorial with LINQ

Demo Code



using System;//from   www  .  j  a  v a2s .  c om
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        // Generate a sequence of integers from 9 .. 1.
        var factors = Enumerable.Range(1, 9).Reverse();
        
        // Accumulate a running total (aggregate) of each factor multiplied by the next factor.
        int factorial = factors.Aggregate((runningTotal, nextFactor) => runningTotal * nextFactor);
        
        Console.WriteLine("9 factorial (9!) is {0}", factorial);

    }
}

Result


Related Tutorials