← All examples

Compound interest

Java Loops

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

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;
    }
}