Question 7: Recursion and loops [10]

Part I: What is the output of this program?

#include <cstdio>

int main()
{
   int counter = 0;
   for (int a = 0; a < 5; a++) {
       int x = 0;
       while (x < a) {
          x++;
          counter++;
       }
   }
   printf("Counter is %d\n", counter);
}

Part II: Rewrite the factorial function so that it uses a loop instead of recursion.

// recursive function calculates n!
int FactorialRecursive(int n)
{
   if (n <= 1) {
       return 1;
   }
   return n * FactorialRecursive(n - 1);
}