C Program to Find Compound Interest

C Program to Find Compound Interest

A school exercise asks you to compound interest. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.

Compounding applies each period’s rate to the current balance, which includes earlier interest.

Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why compound interest uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Show three years of annual compounding.

Compound Interest: follow the data

  1. Given data

    A school exercise asks you to compound interest. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.

  2. Rule

    Compounding applies each period’s rate to the current balance, which includes earlier interest.

  3. Observed result

    Balance: 1102.50; interest: 102.50

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

This annual classroom model multiplies the balance by 1 + 5/100 after each year. It intentionally omits fees, intermediate decimal rounding rules and changing rates.

Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  double principal = 1000.0, balance = principal;
  for (int year = 0; year < 2; year++) balance *= 1.05;
  printf("Balance: %.2f; interest: %.2f\n", balance, balance - principal);
  return 0;
}

Expected output

Balance: 1102.50; interest: 102.50
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  double principal = 1000.0, balance = principal;
  for (int year = 0; year < 2; year++) balance *= 1.05;
  printf("Balance: %.2f; interest: %.2f\n", balance, balance - principal);
  return 0;
}

The first year changes 1000 to 1050.

The second year multiplies 1050 by 1.05 to produce 1102.50.

Follow the worked calculation

  1. Start with the sample

    The first year changes 1000 to 1050.

  2. Apply the rule

    The second year multiplies 1050 by 1.05 to produce 1102.50.

  3. Check the result

    Balance: 1102.50; interest: 102.50

    Balance: 1102.50; interest: 102.50
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  double principal = 1000.0, balance = principal;
  for (int year = 0; year < 2; year++) balance *= 1.05;
  printf("Balance: %.2f; interest: %.2f\n", balance, balance - principal);
  return 0;
}

Show three years of annual compounding.

Worked answer

The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Balance: 1157.62; interest: 157.62. The result should follow from the operations, rather than from memorizing the first sample.

C17 · complete program

#include <stdio.h>
int main(void) {
  double principal = 1000.0, balance = principal;
  for (int year = 0; year < 3; year++) balance *= 1.05;
  printf("Balance: %.2f; interest: %.2f\n", balance, balance - principal);
  return 0;
}

Expected output

Balance: 1157.62; interest: 157.62
Run in C compiler →

Recognize the important boundaries

A displayed two-decimal answer may depend on binary rounding near a tie. Production accounting requires a defined exact representation and rounding policy.

The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why is two-year interest larger than 100?
Choose one answer. You can try again.

Answer and explanation

The second year also earns interest on the first year’s interest.

Correct. The current balance grows before the next multiplication.

Use the idea independently

Compounding applies each period’s rate to the current balance, which includes earlier interest.

Continue with arithmetic operand types.

Continue with validated input.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program for Area and Perimeter of a Rectangle →