diff options
Diffstat (limited to 'reference/C/EXAMPLES/fact2.c')
-rw-r--r-- | reference/C/EXAMPLES/fact2.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/fact2.c b/reference/C/EXAMPLES/fact2.c new file mode 100644 index 0000000..1c8a219 --- /dev/null +++ b/reference/C/EXAMPLES/fact2.c @@ -0,0 +1,29 @@ +/*************************************************************************** + * + * Purpose: generate the factorial of a number by recursion + * Author: M. J. Leslie + * Date: 09-Apr-94 + * + ***************************************************************************/ + +#include <stdio.h> + +int factorial(int num); + +main() +{ + int num; + puts ("This program will return the factorial of a number."); + printf("Please enter the number ==> " ); + scanf("%d", &num); + + printf(" %d! is %d\n",num, factorial(num) ); +} + +int factorial(int num) + { + int ans=1; + if (num == 1 ) return; + ans = num * factorial(num-1); + return ans; +} |