Sunday, 17 May 2015

How to calculate Factorial of a Integer number in c# ?

In this post I wrote a program in c# to calculate a factoral of Integer number using Recursive function and while loop also.

What is Recursive Function:-

A Method can call another methods but it can also call itself. When a mathod calls itself, it'll be named recursive method.
A Recursive usuallly, has the two specifications:

  • Recursive method calls itself so many times until being satisfied.
  • Recursive method has parameter(s) and calls itself with new parameter values.
Source code using Recursive function :-


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

namespace ConsoleDemoApp
{
        class Program
        {
           static void Main(string[] args)
           {
            Console.WriteLine("Enter a integer number");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number <= 0)
            {
                Console.WriteLine("Please enter a positive integer number!");
                Console.ReadKey();
                return;
            }
            long fact = GetFactorial(number);
            Console.WriteLine("{0} factorial is {1}", number, fact);          
            Console.ReadKey();
           }

            // get factorial of integer
         private static long GetFactorial(int number)
         {         
            if (number == 0)
            {
                return 1;
            }
            return number * GetFactorial(number-1);
         }
    }
}


Output:-













Source code using While Loop :-


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

namespace ConsoleDemoApp
{
        class Program
        {
           static void Main(string[] args)
           {
            Console.WriteLine("Enter a integer number");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number <= 0)
            {
                Console.WriteLine("Please enter a positive integer number!");
                Console.ReadKey();
                return;
            }
            long fact = Factorial(number);
            Console.WriteLine("{0} factorial is {1}", number, fact);          
            Console.ReadKey();
           }

            // get factorial of integer using while loop

          static long Factorial(int input)
           {
               int answer = 0, count = 0;
               if (input > 0)
               {
                   count = 1;
                   while (count <= input)
                   {
                       if (count == 1)
                       {
                           answer = 1;
                           count++;
                       }
                       else
                       {
                           answer = count * answer;
                           count++;
                       }
                   }
               }

               return answer;
           }
    }
}


Output:-











Source code using For Loop :-


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

namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            if (number<0)
            {
                Console.WriteLine("Please enter a positive interger value");
                Console.ReadKey();
                return;
            }
            if (number == 0)
            {
                Console.WriteLine("\nFactorial of Given Number is: 1");
                Console.ReadLine();
                return;
            }
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: " + fact);
            Console.ReadLine();

        }
    }
}

Output:-













In the above article I try to explain how to calculate Factorial using Recursive function, while loop and for loop. So I hope its useful to you. Please post your feedback, question, or comments about this article.

No comments:

Post a Comment