Signup/Sign In

C Program to find the Factorial of a Number using Recursion

learn C language tutorial

Logic To Find The Factorial Of A Number Using Recursion:

  • Get the input from the user, by using the entered value the fact() is called,
  • The n-1 value is passed to fact() from the function,
  • Every time the function is called the n value is decremented by 1,
  • Once the value of n is 1, the recursive function will be stopped and send the value to the main() function.

Program To Find The Factorial Of A Number Using Recursion:

#include<stdio.h>
long int fact(int x);
int main() {
    int x;
    printf("Enter A Number To Find Factorial: ");
    scanf("%d",&x);
    printf("The Factorial of %d = %ld", x, fact(x));
    return 0;
}

long int fact(int x) {
    if (x>=1)
        return x*fact(x-1);
    else
        return 1;
}

Output:

fact