Home java factorial in java

factorial in java




This code believes the factorial number n. Why, in order to calculate the factorial of N numbers N, is N * CalcFactorial (n-1)? Why is it just calcfactorial (n)?

Public Long CalcFactorial (int n) {
  if (n == 0) Return 1;

Answer 1, Authority 100%

This is recursion. Whenever the function comes to the RETURN block, it causes itself, but already with the army of N-1. And so until N is equal to 0. n == 0 This is an extreme case, then the function returns 1 and summarizes the previous results.

You should read more about how the Recursion . Perhaps it is also worth reading what factorial is if you ask such a question.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions