Tilbage til slide -- Tastaturgenvej: 'u'  forrige -- Tastaturgenvej: 'p'                recursion/fak-1-side-by-side.c - Den rekursive og den halerekursive fakultetsfunktion - side om side.Lektion 11 - slide 10 : 27
Program 5

#include <stdio.h>

// Prototype
unsigned long int it_fac(unsigned int n,
                                      unsigned int i,
                                      unsigned long int result);

unsigned long int factorial(unsigned int n){
  return it_fac(n, 1, 1);
}

unsigned long int it_fac(unsigned int n,   
                         unsigned int i,
                         unsigned long int result){
  if (i <= n)
     return it_fac(n, i + 1, result * i);
  else
     return result;
}


unsigned long int fac(unsigned int n){
  if (n == 0)
    return 1;
  else return n * fac(n - 1);
}

int main(void) {
  unsigned int k;

  // Upper limits of k:  12  (long is 4 bytes)
  // Upper limits of k:  20  (long is 8 bytes)

  for (k = 0; k <= 12; k++){                     
    printf("%-20lu %20lu\n", k, factorial(k));
    printf("%-20lu %20lu\n", k, fac(k));
  }
  
  return 0;
}