Home » Program for factorial using recursion in C

Program for factorial using recursion in C

Learn about Program for factorial using recursion in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Program for factorial using recursion in C

Program:

#include<stdio.h>

int factorial(int n) 
{
     if(n!=0)
         return n*factorial(n-1);
     else
         return 1; 
}

int main()
{
     int num, result;
     printf("Enter a positive number: ");
     scanf("%d",&num);
     result= factorial(num); //function call
     printf("Result = %d\n",result);
     return 0;
}

Output:

Enter a positive number: 5
Result = 120

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C program to display first n numbers in the Fibonacci series using recursion in C
C program to Find Nth Fibonacci term using Recursion