Skip to content

Compound interest

Java loop over years

Flowchart (ISO 5807)

StartInput p, r, yearsamount = pi = 0, years − 1, 1amount = amount·(1 + r / 100)Return amountEndFigure 1 — Interest.compound

Source code

Java
class Interest {
    static double compound(double p, double r, int years) {
        double amount = p;
        for (int i = 0; i < years; i++) {
            amount = amount * (1 + r / 100);
        }
        return amount;
    }
}

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

Language
Java
Constructs
loop over years
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples