My factorial function does not return the expected value! C language

Question:

My teacher is starting to pass functions, and he asked us to make a factorial function of a number, but it always returns the wrong value and I don't know what happens.

Code:

int fatorial(int n) {
 int ans = 1;

 while(n>1){
  ans*=n; n--; return ans;
 }

}

Answer:

Good friend, from what I've seen your function is 90% correct, however you are returning the ans value before it is completely correct… You must multiply all the numbers and then return it…

Example:

int fatorial(int n) { int ans = 1;

while(n>1){ ans*=n; n--; }
 return ans;
}
Scroll to Top