Friday 10 August 2012

How to calculate the factorial of a number using recursion in C++


Program to calculate the factorial of a number using Recursion



#include<iostream>

#include<conio.h>

using namespace std;

int factorial(int number);

int main()
{
        int number = 0, result = 0;

        cout << "\n\n\t __ Calculate the factorial of a number using Recursion __";

        cout << "\n\n\n  Enter the Number - ";

        cin >> number;

        result = factorial(number);

        cout << "\n\n\t Factorial of " << number << " is " << result;

        getch();

        return 0;

}

int factorial(int number)
{
        if (number == 1)
      
               //__ Condition that will stop the infinite looping __
        {
               return 1;
        }
        else
        {
               return number * factorial (number - 1); //__ Recursion __
        }
}

No comments:

Post a Comment