C - Recursion - Classical recursion of a factorial

A classical example of recursions is of course the factorial one.

If you want to know the result of a factorial, do not hesitate to use a recursive function.

Let's see it:

#include        <stdio.h>

int     doIt(int nb) {
  if (nb <= 1)
    return 1;
  printf("nb = %d\n", nb);
  return nb * doIt(nb - 1);

int     main() {
  int first = 5;

  printf("Last = %d\n", doIt(first));
  return 0;

The render:

nb = 5
nb = 4
nb = 3
nb = 2
Final result = 120


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.