Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Recursion
  6. Finding Factorial using Recursion

Finding Factorial using Recursion

Finding Factorial using Recursion

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

Output

Enter number 5
Factorial of 5 is 120

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments