Sunday, 11 October 2020

Factorial of a Number

Step 1: Open Visual Studio and select console application.



Step 2: Write a Program to find factorial of number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace NumberFactorialConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;
 
            Console.WriteLine("Enter a number to get factorial of number");
            number = int.Parse(Console.ReadLine());
 
            if (number > 0)
            {
                int factorial = new Program().MathFactorial(number);
                Console.WriteLine("Factorial of " + number + " is: " + factorial);
            }
            else
            {
                Console.WriteLine("Number must be greater than zero");
            }
            Console.ReadLine();
        }
 
        /// <summary>
        /// This method is used for get factorial of number
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public int MathFactorial(int number)
        {
            return (number == 1) ? 1 : number * MathFactorial(number - 1);
        }
    }
 
}

Step 3 : Run the application then console will be open.
Step 4: Enter the string then press "Enter" key then see the output.




No comments:

Post a Comment

Factorial of a Number

Recently Viewed