Pages

Tuesday, 16 June 2015

Compute factorial of any number using recursion

Compute factorial of any number using recursion

Coding:

#include<stdio.h>
int fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}
void main()
{
int n;
printf("Please enter the number:");
scanf("%d",&n);
printf("Result: %d!=%d",n,fact(n));
}

Output:
Please enter the number:6
Result: 6!=720