mercredi 2 novembre 2016

I am trying to implement a program recursively. I just don't understand y my last line is being executed twice

Why is Hi being printed 4 times. It will just be executed once before the function finally returns the complete value

 #include <stdio.h>
    int k=0;
    int factorial(unsigned int i) {

       if(i <= 1) {
          return 1;
       }
       else
       {
           k=i * factorial(i - 1);
       }
      printf("hi"); // Why is Hi being printed 4 times. 
       return k;


     }

    int  main() {
       int i = 5;
              printf("Factorial of %d is %d\n", i, factorial(i));
       return 0;
    }

Aucun commentaire:

Enregistrer un commentaire