Skip to content

Digit sum

PHP recursion over digits

Same task in: Java

Flowchart (ISO 5807)

YesNoStartInput nn = 0Return 0Return n mod 10 + digitSum(intdiv(n, 10))EndFigure 1 — digitSum

Source code

PHP
function digitSum($n) {
    if ($n == 0) return 0;
    return $n % 10 + digitSum(intdiv($n, 10));
}

The editor opens with this code — change it and the chart redraws.

Language
PHP
Constructs
recursion over digits
Topic
Recursion
Formats
Word, Visio, draw.io, PDF, SVG

Related examples