Created
November 17, 2011 20:19
-
-
Save tiagodavi/1374392 to your computer and use it in GitHub Desktop.
Função recursiva que calcula o fatorial de um número
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Função recursiva que calcula o fatorial de um número | |
function fatorial($numero){ | |
if($numero <= 1){ | |
return $numero; | |
}else{ | |
return $numero * fatorial($numero - 1); | |
} | |
} | |
//4*3*2 = 24 | |
echo fatorial(4); |
Sorry, this the right:
function fatorial ($n) {
return $n > 1 ? $n * fatorial($n - 1) : 1;
};
echo fatorial(4);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not: