Programming Examples

C program to find the factorial using recursion


Write program to compute factorial of a user input number using recursive function.

Solution

#include<stdio.h>
int factorial(int);
int main()
{
	int num,ans;
	printf("Enter any number : ");
	scanf("%d",&num);
	ans=factorial(num);
	printf("Factorial of %d is : %d",num,ans);
	return 0;
}
int factorial(int n)
{
	if(n==1)
		return 1;
	else 
		return n*factorial(n-1);
}
Output

Enter any number : 5

Factorial of 5 is : 120

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad