Simple program #10 (Source code to calculate
factorial of a number)
            
Enter a number: 5
Factorial of 5 is: 120
The program is written in C#
programming language and will accept a number as input. The logic of the
program is to find factorial of the input operator.
Let’s find out a simple and easy way
to code the program.
Practical Implementation: 
using System;
namespace patternProblem.Simple_program
{
    class Simple10   //factorial of a number
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            int n = Convert.ToInt32(Console.ReadLine());
            int factorial = 1;
            if (n >= 0)
            {
                for (int i = n; i
> 1; i--)
                {
                    factorial = factorial * i;
                }
                Console.WriteLine("Factorial of {0} is:
{1}", n, factorial);
            }
            else 
            {
                Console.WriteLine("Factorial is -ve number is
not possible");
            }
            Console.ReadKey();
        }
    }
}
Output:
The input
number is 5. The program will do find factorial of the input operator. The
output is shown below:
Enter a number: 5
Factorial of 5 is: 120
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table Simple program
Keep learning and sharing...
                    Technologies
No comments:
Post a Comment