programming-examples/c/Recursion/C Program to Find the Factorial of a Number using Recursion.c

35 lines
691 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* C Program to find factorial of a given number using recursion
*/
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}