C Program to Calculate Compound Interest

C Program to Calculate Compound Interest

Compound interest is a common concept in finance where the interest earned is added back to the principal, and future interest is calculated on this new amount. Unlike simple interest, where interest is calculated only on the principal, compound interest grows faster and is used in savings accounts, loans, and investments. Writing a program to calculate compound interest in C helps beginners practice mathematical operations, loops or exponentiation, and understanding formulas in programming.

The formula for compound interest is:

Compound Interest = Principal × (1 + Rate / 100) ^ Time – Principal

Where Principal is the initial amount, Rate is the annual interest rate, and Time is the number of years. By implementing this formula in C, we can automatically calculate the interest and total amount for any given principal, rate, and time period.

C Program to Calculate Compound Interest Using pow()

Below is a simple C program that calculates compound interest using the pow() function from math.h.

#include <stdio.h>
#include <math.h>

int main() {

    double principal, rate, time, compoundInterest;

    printf("Enter principal amount: ");
    scanf("%lf", &principal);

    printf("Enter annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter time in years: ");
    scanf("%lf", &time);

    compoundInterest = principal * pow((1 + rate / 100), time) - principal;

    printf("Compound Interest = %.2lf\n", compoundInterest);

    return 0;

}

In this program, we use double variables to handle decimal values accurately. We include the math.h library to use the pow() function for exponentiation. First, we ask the user to input the principal, interest rate, and time in years using scanf(). Then, we calculate the compound interest using the formula principal * pow((1 + rate / 100), time) - principal. Finally, we display the result using printf().

C Program to Calculate Compound Interest Without pow()

For beginners who want to avoid pow(), we can calculate compound interest using a loop. This method repeatedly multiplies the principal by (1 + rate/100) for each year.

#include <stdio.h>

int main() {

    double principal, rate, time, amount;
    int i;

    printf("Enter principal amount: ");
    scanf("%lf", &principal);

    printf("Enter annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter time in years: ");
    scanf("%lf", &time);

    amount = principal;

    for(i = 0; i < time; i++) {
        amount = amount + (amount * rate / 100);
    }

    double compoundInterest = amount - principal;

    printf("Compound Interest = %.2lf\n", compoundInterest);

    return 0;

}

Here, we use a for loop to add interest for each year. We start with amount = principal and for each year, we calculate amount + (amount * rate / 100). After the loop, the total compound interest is amount - principal. This method demonstrates how loops can be used for repetitive calculations in C.

Common Beginner Mistakes

When calculating compound interest in C, beginners often make these mistakes:

  • Using int instead of float or double, which can cause loss of decimal precision.
  • Applying the simple interest formula instead of the compound interest formula, forgetting that interest is calculated on the accumulated amount over time.

To avoid these issues, always use the correct data type for decimals and ensure your formula reflects the compound interest calculation: amount = principal * pow(1 + rate / 100.0, time).

FAQs

Q1: Can this program handle fractional years?
Yes, using double allows you to handle fractional years, although using loops works best for whole years. For fractional years, using pow() is recommended.

Q2: How can I calculate the total amount including interest?
You can calculate the total amount as totalAmount = principal + compoundInterest;.

Q3: Can I calculate compound interest compounded monthly or quarterly?
Yes, adjust the formula to account for compounding frequency: amount = principal * pow(1 + (rate / n), n * time), where n is the number of compounding periods per year.

Conclusion

Calculating compound interest in C is a great way to practice using formulas, loops, exponentiation, and variable handling. By implementing this program, beginners learn how to automate financial calculations and understand the difference between simple and compound interest. You can extend this program to handle multiple compounding frequencies, display yearly balances, or include user-friendly menus to make it more interactive.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: C Program For Compound Interest – Example and explanation.
  3. Programiz: C Math Functions – Learn about pow() in C.
  4. TutorialsPoint: C Input/Output – Basics of scanf() and printf() in C.
Scroll to Top