All articles
c

A program to find factorial of a number using RECURSION

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 085

  • Write a program to find factorial of a number using RECURSION

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

Comments