summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/fact2.c
blob: 1c8a2190162cca83a2e264f14563a04215f0c487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}