Home

A program to find factorial of a number using RECURSION

C Programming Language 085

Program

#include <stdio.h>

long int fact(int);
void main()
{
    int n;
    long int f;

    printf("Enter a number: ");
    scanf("%d", &n);

    f = fact(n);
    printf("Factorial = %ld", f);

    printf("\n");
}

long int fact(int n)
{
    if (n == 0 || n == 1)
    {
        return (1);
    }
    else
    {
        return (n * fact(n - 1));
    }
}

Result

Enter a number: 4
Factorial = 24


Last Updated on

Next Post: Rules vs Guidelines →

Comments