Sunday, 15 December 2013

C# program to find the factorial of a number



In this article we will discuss with you the program for calculating the factorial of a number using console application in c#.net:
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Read an input from the User in this Variable “int_Number” for storing that number whose factorial you want to find out.
Step3: Now if that input number is 1 or 0 then its factorial will be 1 otherwise we have to loop from the input number till 1 and multiply the input no with its immediate lower number till 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeNo
{
    class Program
    {

        static void Main()
        {
           
            int int_number;
            long ln_num;
            ln_num = 1;


            Console.WriteLine("please enter the number whose factorial you want to find");
            int_number = int.Parse(Console.ReadLine());

            if ((int_number == 1) || (int_number == 0))
            {
                Console.WriteLine("factorial of the number "+int_number+ " is 1.");
            }
            else
            {
                for (int i = int_number; i >= 1; i--)
                {
                    ln_num = ln_num * i;
                }
                Console.WriteLine("factorial of the number " + int_number + " is " +ln_num );
            }

            Console.ReadLine();
       }
    }

}

Output:-


No comments:

Post a Comment