C Programming Examples Tutorial Index

C Number Programs

This C programming tutorial will guide you on how to calculate compound interest. Compound interest is an essential financial concept for investment growth, loan repayments, and future value estimation. By the end of this tutorial, you will have a solid understanding of how to compute compound interest using C programming.



C Program to Calculate Compound Interest

Example Program:

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

int main() {
    // Initialize variables for the principal amount, interest rate, time, 
    // number of compounding periods, compound interest (CI), and final amount (A)
    double principle, rate, time, n, CI, A;

    // Prompt the user to enter the principal amount
    printf("Enter the principal amount: ");
    // Take the principal amount as input
    scanf("%lf", &principle);

    // Prompt the user to enter an annual interest rate in percentage (for example, 5% for five years).
    printf("Enter the annual interest rate (e.g., for 5%%, enter 5): ");
    // Take the annual interest rate as input
    scanf("%lf", &rate);
    // Convert the entered interest rate to a decimal
    rate = rate / 100;

    // Prompt the user to enter the time in years
    printf("Enter the time (in years): ");
    // Take the time as input
    scanf("%lf", &time);

    // Prompt the user to enter the number of times that interest is compounded per year
    printf("Enter the number of times that interest is compounded annually: ");
    // Take the number of compounding periods as input
    scanf("%lf", &n);

    // Calculate the final amount after time 't' with compound interest
    A = principle * pow((1 + rate / n), n * time);
    // Calculate the compound interest by subtracting the principal from the final amount
    CI = A - principle;

    // Print the compound interest
    printf("The compound interest is: %.2lf\n", CI);

    return 0;
}

Program Output:

Enter the principal amount: 1000
Enter the annual interest rate (e.g., for 5%, enter 5): 8
Enter the time (in years): 6
Enter the number of times that interest is compounded annually: 4
The compound interest is: 608.44

Compound Interest Formula

Following is the mathematical formula for compound interest:

A = P(1 + r/n) ^ (nt)

The mathematical formula needs to be slightly modified in C programming because C uses different symbols for exponentiation and requires explicit multiplication. The equivalent code in C would be:

A = P * pow((1 + r/n), (n*t));

Where:

  • A is the total amount after n years, including interest.
  • P is the principal amount (the initial amount of invested capital).
  • pow() is a C function for exponentiation.
  • r is the interest rate in decimal.
  • n is the number of yearly interest compounding.
  • t is the investment duration in years.

Compound Interest Example

Assume you invest Rs 5000 (p) at an annual rate of interest of 8% (r = 0.08) for five years (t = 5), compounded quarterly (n = 4). Filling in these values in the formula results in the following:

P = 5000
r = 8% = 0.08
t = 5 years
n = 4 (compounded quarterly)

A = 5000 * (1 + 0.08/4)^(4*5)

After evaluating the above expression, your investment's future value (A) would be approximately 7429.74. Thus, you would have earned 2429.74 in interest over five years.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram