Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Factorial

  • Define the signature of the function before it is being used.
  • Then we can implement the function anywhere later.
#include<stdio.h>

int iterative_factorial(int n);
int recursive_factorial(int n);

int main() {
    printf("Iterative factorial of 5: %d\n", iterative_factorial(5));
    printf("Recursive factorial of 5: %d\n", recursive_factorial(5));
    return 0;
}

int iterative_factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int recursive_factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * recursive_factorial(n - 1);
    }
}