Are you searching to find the sum of natural numbers up to n using recursion in PHP? Use the below PHP code to find the sum of natural numbers up to n.
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Find the sum of natural numbers up to n. */ function recurSum($n) { if ($n <= 1) return $n; return $n + recurSum($n - 1); } $n = 5; echo(recurSum($n)); |
Program Output
1 | 15 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.