Skip to content

Sum 1..n (recursion)

Java recursion

Flowchart (ISO 5807)

YesNoStartInput nn = 0Return 0EndReturn n + sumTo(n − 1)EndFigure 1 — R.sumTo

Source code

Java
class R {
    static int sumTo(int n) {
        if (n == 0) {
            return 0;
        }
        return n + sumTo(n - 1);
    }
}

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

Language
Java
Constructs
recursion
Topic
Recursion
Formats
Word, Visio, draw.io, PDF, SVG

Related examples