Browse Source

refactoring: change variable names, format code and attach some descriptive comments in calculatorFactorial.c

remotes/origin/feature/calculator-factorial
fdai7514 2 years ago
parent
commit
0c34fa9c5b
  1. 13
      src/calculatorFactorial.c
  2. 2
      src/calculatorFactorial.h

13
src/calculatorFactorial.c

@ -1,6 +1,13 @@
#include "calculatorFactorial.h" #include "calculatorFactorial.h"
int calculatorFactorial(int x)
int calculatorFactorial(int num) //implement recursion. The function calls itself so many times, till the breaking condition is fulfilled.
{ {
if (x==0)return 1;
else return x*calculatorFactorial(x-1);
if (num == 0) //breaking condition
{
return 1;
}
else
{
return num * calculatorFactorial(num - 1); //If its not breaking condition, then multiply the number with the same function implemented on the previous number. Eventually it will reach breaking condition.
}
} }

2
src/calculatorFactorial.h

@ -3,6 +3,6 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
int calculatorFactorial(int x);
int calculatorFactorial(int num);
#endif // CALCULATORFACTORIAL_H #endif // CALCULATORFACTORIAL_H
Loading…
Cancel
Save