Saturday 28 September 2024

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



#include <stdio.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, fact;


    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &number);


    // Function call and output

    fact = factorial(number);

    printf("Factorial of %d is %d", number, fact);


    getch(); // Wait for user input to close the program

    return 0;