C Program to Calculate Compound Interest

1. Introduction

In the realm of finance, understanding the growth of investments is pivotal. Compound Interest, often termed the "eighth wonder of the world", depicts the scenario where interest accumulates on both the initial principal and on the accrued interests from preceding periods. In this guide, we will learn how to calculate Compound Interest using C programming language.

2. Program Overview

The program will:

1. Request the user for the principal amount, rate of interest, time period, and number of times interest is compounded annually.

2. Derive the Compound Interest using the customary formula.

3. Display the accumulated amount, inclusive of the principal and interest, to the user.

3. Code Program

#include <stdio.h>  // Incorporate the Standard I/O library
#include <math.h>   // Integrate the Math library for power function

int main() {  // Commence main function

    float principal, rate, time, n, amount, interest;  // Variables declaration

    // Gather user input for principal, rate of interest, time, and compound frequency
    printf("Enter Principal Amount ($): ");
    scanf("%f", &principal);

    printf("Enter Rate of Interest (%%): ");
    scanf("%f", &rate);

    printf("Enter Time (years): ");
    scanf("%f", &time);

    printf("Enter Number of Times Interest Applied per Time Period: ");
    scanf("%f", &n);

    // Compute Compound Interest using the formula: P(1 + r/n)^(nt)
    amount = principal * pow((1 + rate / (100 * n)), (n * time));
    interest = amount - principal;

    // Display the accumulated amount and the interest
    printf("Total Amount (Principal + Compound Interest): $%.2f\n", amount);
    printf("Compound Interest accrued: $%.2f\n", interest);

    return 0;  // Terminate the program gracefully

}

Output:

Enter Principal Amount ($): 1000
Enter Rate of Interest (%): 5
Enter Time (years): 2
Enter Number of Times Interest Applied per Time Period: 4
Total Amount (Principal + Compound Interest): $1104.89
Compound Interest accrued: $104.89

4. Step By Step Explanation

1. #include <stdio.h> and #include <math.h>: These lines rope in the standard input/output and mathematical libraries, respectively.

2. int main(): The central function initiating the program's execution.

3. Variable Declaration:

- The variables will house user inputs and the calculated values.

4. User Input: We guide the user to provide the necessary details: principal amount, rate, time, and frequency of compounding.

5. Compound Interest Calculation:

The quintessential formula for Compound Interest is: A = P(1 + r/n)^(nt) 

Where:

  • A is the future value of the investment/loan, including interest
  • P is the principal investment/loan amount
  • r is the annual interest rate (decimal form)
  • n is the number of times interest is compounded per year
  • t is the number of years the money is invested/borrowed for

Our code employs this formula to discern the total amount, and by subtracting the principal, we obtain the compound interest.

6. Display Results: The derived total amount and compound interest are presented to the user. 

With the magic of compounding brought to life through this program, one gains clearer insights into how investments grow over time.

Comments