Skip to content

Digital root

C++ nested loops

Flowchart (ISO 5807)

YesYesNoNoStartInput nn ≥ 10s = 0n > 0s = s + n mod 10n = n / 10n = sReturn nEndFigure 1 — digitalRoot

Source code

C++
int digitalRoot(int n) {
    while (n >= 10) {
        int s = 0;
        while (n > 0) {
            s += n % 10;
            n /= 10;
        }
        n = s;
    }
    return n;
}

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

Language
C++
Constructs
nested loops
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples