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

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

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

//the computation
foreach (var number in Enumerable.Range(1, input).ToList()) fact *= number;

//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