Monday, 9 September 2024

Write a program to compute the factorial of a number using recursion.

 


#include <iostream.h>

#include<conio.h>

// Recursive function to calculate factorial

int factorial(int n)

 {

    if (n <= 1)

       {

        return 1;

    } else

       {

        return n * factorial(n - 1);

    }

}


int main()

 {

    int number;

    int fact;


    // Input

    cout << "Enter a positive integer: ";

         cin >> number;


    // Function call and output

    fact = factorial(number);

    cout << "Factorial of " << number << " is " << fact ;

      getch();

    return 0;

}