C Program to Calculate Compound Interest

Calculating Compound Interest is a crucial concept in both finance and programming. In C programming, understanding how to compute compound interest can enhance your problem-solving skills and prepare you for more advanced financial calculations. This article will provide a comprehensive guide to writing a C program to calculate compound interest. We’ll explore three distinct methods, complete with code examples, detailed explanations, and sample outputs. You may also like our article on Simple Interest.

Write a C Program to Calculate Compound Interest

What is Compound Interest?

Compound interest calculates the interest on the initial principal and the accumulated interest from previous periods. Unlike simple interest, which calculates interest only on the principal amount, compound interest includes the interest accrued over time. The formula to calculate compound interest is:

A=P(1+r/n)nt

Where:

  • A is the total amount after time t.
  • P is the principal amount (initial investment).
  • r represents the annual interest rate (expressed as a decimal).
  • n represents the number of times the interest is compounded per year.
  • t is the time the money is invested for, in years.

The compound interest is then calculated as:

Compound Interest=A−P

Method 1) C Program to Calculate Compound Interest Using the Standard Compound Interest Formula

Code Example

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

int main() {
    double principal, rate, time, compoundInterest;
    int n;

    // Input values
    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);
    printf("Enter number of times interest is compounded per year: ");
    scanf("%d", &n);

    // Calculate compound interest
    double amount = principal * pow((1 + rate / (100 * n)), n * time);
    compoundInterest = amount - principal;

    printf("Compound Interest after %.2lf years is: %.2lf\n", time, compoundInterest);

    return 0;
}

Explanation of the Code

This method uses the standard compound interest formula to compute the total amount after a certain period. The program leverages the pow function from the math.h library to calculate the power of the expression. After computing the total amount, the program subtracts the principal to find the compound interest.

Output

Enter principal amount: 1000
Enter annual interest rate (in percentage): 5
Enter time in years: 3
Enter number of times interest is compounded per year: 4
Compound Interest after 3.00 years is: 159.27

Method 2) C Program to Calculate Compound Interest Using a Loop

Code Example

#include <stdio.h>

int main() {
    double principal, rate, time, compoundInterest;
    int n, i;

    // Input values
    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);
    printf("Enter number of times interest is compounded per year: ");
    scanf("%d", &n);

    compoundInterest = principal;
    double rate_per_period = rate / (100 * n);
    int total_periods = n * time;

    // Calculate compound interest using a loop
    for(i = 0; i < total_periods; i++) {
        compoundInterest += compoundInterest * rate_per_period;
    }

    compoundInterest -= principal;

    printf("Compound Interest after %.2lf years is: %.2lf\n", time, compoundInterest);

    return 0;
}

Explanation of the Code

In this method, the program calculates compound interest iteratively. Each iteration updates the principal to include the interest earned during that period. This approach avoids using the pow function and gives a more granular view of how the principal grows over time.

Output

Enter principal amount: 1000
Enter annual interest rate (in percentage): 5
Enter time in years: 3
Enter number of times interest is compounded per year: 4
Compound Interest after 3.00 years is: 159.27

Method 3) C Program to Calculate Compound Interest using Function

Code Example

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

// Function to calculate compound interest
double calculateCompoundInterest(double principal, double rate, double time, int n) {
    double amount = principal * pow((1 + rate / (100 * n)), n * time);
    return amount - principal;
}

int main() {
    double principal, rate, time, compoundInterest;
    int n;

    // Input values
    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);
    printf("Enter number of times interest is compounded per year: ");
    scanf("%d", &n);

    // Calculate compound interest using the function
    compoundInterest = calculateCompoundInterest(principal, rate, time, n);

    printf("Compound Interest after %.2lf years is: %.2lf\n", time, compoundInterest);

    return 0;
}

Explanation of the Code

This method encapsulates the compound interest calculation in a separate function, calculateCompoundInterest. By modularizing the calculation, the code becomes easier to maintain and reuse. The function returns the compound interest, simplifying the logic in the main function.

Output

Enter principal amount: 1000
Enter annual interest rate (in percentage): 5
Enter time in years: 3
Enter number of times interest is compounded per year: 4
Compound Interest after 3.00 years is: 159.27

You can calculate compound interest in C using various methods, each offering unique advantages. The standard formula method provides a direct approach using mathematical functions, the iterative method offers a detailed view of interest accumulation, and the modular method promotes code reusability. Understanding these techniques equips you with a versatile skill set for handling compound interest calculations in C programming.

Categories C

Leave a Comment