Program to find the factorial of a number using foreach extension method in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1602
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int input = 10; //Factorial of the neumber to find
int fact = 1; //a temporary variable to store the intermediate computed values

//computation
Enumerable
.Range(1, input)
.ToList()
.ForEach(i=> fact *= i);

//display the result
Console.WriteLine("Factorial of {0} is {1}", input, fact);
Console.ReadKey();
}
}
}



Output
-------------

Factorial of 10 is 3628800

Comments or Responses

Login to post response