Factorial Number Program in C#

The factorial, symbolized by an exclamation mark (!), is a quantity defined for all integer s greater than or equal to 0.

For an integer n greater than or equal to 1, the factorial is the product of all integers less than or equal to n but greater than or equal to 1. The factorial value of 0 is defined as equal to 1. The factorial values for negative integers are not defined. Let's see a C# language program that show factorial value of user input number.

using System;

namespace FactorialExample

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter a number");

            int number = Convert.ToInt32(Console.ReadLine());

            long fact = GetFactorial(number);

            Console.WriteLine("{0} factorial is {1}", number, fact);           

            Console.ReadKey();

        }

 

        private static long GetFactorial(int number)

        {

            int value = 1;

            if (number == 0)

            {

                return 1;

            }

            else

            {

                for (int i = number; i >=1; i--)

                {

                    value *= i;

                }

            }

            return value;

        }

    }

}

Ebook Download
View all
Learn
View all