Compound Interest: flowchart, algorithm and program

This program reads the principal, the yearly rate and the number of years, then finds the compound interest. It multiplies the amount by (1 + rate/100) once for every year, and the interest is the final amount minus the principal.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read principal, rate, years.
  3. Step 3: Set amount = principal.
  4. Step 4: Set i = 1.
  5. Step 5: If i <= years, go to step 6; otherwise go to step 8.
  6. Step 6: Set amount = amount * (1 + rate/100).
  7. Step 7: Increase i by 1. Go to step 5.
  8. Step 8: Set interest = amount - principal.
  9. Step 9: Print interest.
  10. Step 10: Stop.

Pseudocode

BEGIN
    READ principal, rate, years
    amount ← principal
    i ← 1
    WHILE i <= years DO
        amount ← amount * (1 + rate/100)
        i++
    END WHILE
    interest ← amount - principal
    PRINT interest
END

Program in C, Python and Java

Compound Interest program in C

#include <stdio.h>

int main() {
    float principal, rate, amount, interest;
    int years, i;

    printf("Enter principal: ");
    scanf("%f", &principal);
    printf("Enter rate: ");
    scanf("%f", &rate);
    printf("Enter years: ");
    scanf("%d", &years);

    amount = principal;
    for (i = 1; i <= years; i++) {
        amount = amount * (1 + rate / 100);
    }

    interest = amount - principal;
    printf("Compound Interest = %.2f\n", interest);
    return 0;
}

Compound Interest program in Python

principal = float(input("Enter principal: "))
rate = float(input("Enter rate: "))
years = int(input("Enter years: "))

amount = principal
for i in range(1, years + 1):
    amount = amount * (1 + rate / 100)

interest = amount - principal
print("Compound Interest = %.2f" % interest)

Compound Interest program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double principal, rate, amount, interest;
        int years, i;

        System.out.print("Enter principal: ");
        principal = sc.nextDouble();
        System.out.print("Enter rate: ");
        rate = sc.nextDouble();
        System.out.print("Enter years: ");
        years = sc.nextInt();

        amount = principal;
        for (i = 1; i <= years; i++) {
            amount = amount * (1 + rate / 100);
        }

        interest = amount - principal;
        System.out.printf("Compound Interest = %.2f%n", interest);
    }
}

Sample input: principal = 1000, rate = 10, years = 2
Output: Compound Interest = 210.00

How it works

  • Start with amount = principal, so before year 1 nothing has been added yet.
  • The for loop runs from i = 1 to i <= years, so it repeats exactly once per year.
  • Each pass does amount = amount * (1 + rate / 100). The interest earned so far is added to the base, so next year's interest is calculated on the larger amount. That is what makes it 'compound'.
  • After the loop, interest = amount - principal gives only the interest, not the total amount.
  • Dry run with 1000, 10, 2: year 1 gives 1000 * 1.1 = 1100, year 2 gives 1100 * 1.1 = 1210, so interest = 1210 - 1000 = 210.00.

Dry run (trace table)

StepLineBoxprincipalrateyearsamountiinterestOutput
13Start
212Read principal, rate, years1000102Enter principal: Enter rate: Enter years:
314amount = principal10001021000
415i = 1100010210001
515i <= years?100010210001
616amount = amount * (1 + rate/100)100010211001
715i++100010211002
815i <= years?100010211002
916amount = amount * (1 + rate/100)100010212102
1015i++100010212103
1115i <= years?100010212103
1219interest = amount - principal100010212103210
1320Print interest100010212103210Compound Interest = 210.00
1422Stop100010212103210

Time and space complexity

Time: O(n) · Space: O(1). The loop runs once per year (n = years) doing constant work each time, using only a few variables.

Common mistakes

  • Printing amount instead of amount - principal, which shows the total money and not the interest.
  • Writing rate / 100 without keeping the 1 + part, so amount shrinks to a tiny value instead of growing.
  • Using integer variables for rate or principal, which breaks the division or loses decimals (for example, 5 / 100 becomes 0 in integer math).

Viva questions

Q1. Why do you write amount = principal before the loop?

The amount must start as the original principal, because each year's compounding multiplies the previous amount. Without it, amount would hold a garbage value and the result would be wrong.

Q2. What does the line amount = amount * (1 + rate / 100) do?

It adds one year of interest to the current amount. rate/100 converts the percentage into a fraction, so with a rate of 10 the amount is multiplied by 1.10 each year.

Q3. What is the output if years is 0, and what if years is negative?

For 0 or a negative number, the condition i <= years is false at the start, so the loop never runs. Amount stays equal to principal and the interest printed is 0.00.

Q4. Why is interest calculated as amount - principal, and not just printing amount?

Amount is the total money after compounding, which includes the original principal. Compound interest is only the extra money earned, so we subtract the principal.

Q5. Why does rate / 100 not cause an integer division problem here?

rate is a float, so rate / 100 is a floating-point division and keeps the decimal part. If rate were an int, 5/100 would give 0 and the interest would be lost.

Q6. What is the difference between simple and compound interest, as shown in this program?

Simple interest is calculated on the original principal only, every year. In compound interest, each year's interest is added to the amount, so the next year's interest is calculated on the larger amount.

Q7. What is the time complexity of this program?

It is O(n), where n is the number of years, because the loop runs once per year with constant work inside. Space use is O(1).

Q8. What would you change to compound twice a year (half-yearly), or to avoid the loop?

For half-yearly compounding, use rate/2 per period and run the loop for years*2 periods. To avoid the loop, use the formula amount = principal * pow(1 + rate/100, years) from math.h, which makes it O(1).

Q9. Why might the printed result differ slightly from a calculator, and how could you improve it?

float has only about 6 to 7 digits of precision, so rounding errors build up with large principal or many years. Using double instead of float, with %lf in scanf, gives more accurate results.

Frequently asked questions

What is the difference between simple and compound interest in this code?

Simple interest uses the original principal every year (P*R*T/100). Here the loop multiplies the growing amount each year, so interest is earned on previous interest too.

Can I use pow() instead of a loop?

Yes. amount = principal * pow(1 + rate / 100, years) gives the same result, but you need to include math.h. The loop shows clearly what happens each year.

Why is the answer 210 and not 200 for 1000, 10%, 2 years?

Simple interest would give 200. With compounding, year 2 earns 10% on 1100, which is 110, so the total is 100 + 110 = 210.

More programs

← All programs

Try AI Flowchart free →