Factorial in C program with Recursion

This C program will show, how to print factorial values using recursion by taking user input.

Factorial in C by recursion without taking user input

#include<stdio.h>
int main()
{
  int result =  fact (5);
  printf("Factorial of 5 = %d", result);
}

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

Output

Factorial of 5 = 120

Factorial in C by recursion taking user input

#include<stdio.h>
int main()
{
  int x;
  printf("Enter the number of factorial: ");
  scanf("%d",&x);
  int result =  fact (x);
  printf("Factorial of 5 = %d", result);
}

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

Output

Enter the number of factorial: 5
Factorial of 5 = 120

Thanks for learning with us.

Leave a Comment