Back

Compound Interest Program in C

28 Feb 2025
6 min read

Compound interest is a fundamental financial concept that plays a crucial role in personal finance, investment strategies, and economic planning. Unlike simple interest, which is calculated only on the principal amount, compound interest considers the interest accumulated over previous periods. This compounding effect can enhance the growth of an investment or debt over time. Compound interest can be calculated using a formula or using different coding logics for compound interest programs in c, explored later in this article. Programming certainly makes it easier to complete the daunting task of making elaborate calculations in the compound interest formula manually.

What is Compound Interest?

Compound interest is the interest calculated on the initial principal and also on the accumulated interest from previous periods. It is often referred to as "interest on interest". The frequency of compounding (e.g., annually, semi-annually, quarterly, or monthly) significantly impacts the total amount accumulated over time. 

Imagine you lend your friend one chocolate, When you tell your friend that for every 10 days they delay in returning a chocolate, they owe you an extra chocolate, it mirrors how interest accrues over time. 

In traditional compound interest, the interest earned on an investment is added to the principal, which then becomes the new base for calculating future interest. This creates a situation where the more your friend delays, the more chocolates you accumulate—similar to how your investment grows larger over time as interest compounds on both the original amount and the interest that has already been added. 

In the above example, if you lend your friend one chocolate and they delay buying you a return chocolate for 30 days, here's how it looks:

For the first 10 days: Your friend owes you 1 chocolate (the original).

After 20 days: They owe you an extra chocolate (total now 2).

After 30 days: They owe another extra chocolate for both the first and the second delay (total now 4).

So, after 30 days, your friend owes you 4 chocolates in total. This illustrates how delays add up, similar to how compound interest accumulates over time.

Simple Interest vs. Compound Interest 

Simple Interest remains constant, while Compound Interest grows faster due to interest that keeps accumulating over time and increases. 

Here are some key differences between simple interest and compound interest:

Feature Simple interest Compound interest
Formula SI = (P × R × T) / 100 CI = P (1 + R/100)n - P
Growth manner Linear (Fixed interest each period) Exponential (Interest on interest)

Simple interest is usually applied for short term loans and bonds, whereas compound interest is applied for savings accounts and investments.

Some important features of compound interest are:

  1. Principal Amount: The initial sum of money invested or borrowed.  The principal amount serves as the base on which interest is calculated. Larger principal amounts result in greater interest accumulation over time, as the compounding effect increases with a higher starting point.
  2. Rate of Interest: The percentage at which the money grows annually.
  3. Time Period: The duration for which the money is invested or borrowed. Longer time periods significantly increase the compounding effect, as interest accumulates on the growing amount over time.
  4. Frequency of Compounding: Determines how often the interest is calculated and added to the principal.

Compound Interest Formula

The standard formula for calculating compound interest is:

\( A = P \left( 1 + \frac{r}{n} \right)^{nt} \)

where:

  • A = Final amount (Principal + Interest)
  • P = Principal amount
  • r = Annual interest rate (in decimal form)
  • n = Number of compounding periods per year
  • t = Time in years

Explanation with Pseudo Code

  • Read the principal amount P.
  • Read the annual interest rate r (percentage form, convert to decimal by dividing by 100).
  • Read the number of compounding periods per year n.
  • Read the time duration in years t.
  • Calculate the compound interest factor.
  • Raise this factor to the power of n×t.
  • Multiply the principal amount by the final factor.
  • Print the final amount A.

Pseudo Code

START
    INPUT P, r, n, t
    r = r / 100  // Convert percentage to decimal
    CI_factor = 1 + (r / n)
    power = n * t
    final_factor = CI_factor ^ power
    A = P * final_factor
    PRINT "Final Amount: ", A
END

C Program to Calculate Compound Interest Using the Standard Compound Interest Formula

This C program demonstrates the calculation of compound interest using the standard formula. It allows users to input the principal amount, interest rate, time period, and compounding frequency for accurate results.

Code

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

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

    // Input values
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    
    printf("Enter Annual Interest Rate (in percentage): ");
    scanf("%lf", &rate);
    
    printf("Enter Time Period (in years): ");
    scanf("%lf", &time);
    
    printf("Enter Compounding Frequency (1 for Annually, 2 for Semi-Annually, 4 for Quarterly, 12 for Monthly, 365 for Daily): ");
    scanf("%d", &frequency);

    // Compound interest calculation
    amount = principal * pow((1 + (rate / (frequency * 100))), (frequency * time));
    compoundInterest = amount - principal;

    // Output results
    printf("\nFinal Amount: %.2lf\n", amount);
    printf("Compound Interest: %.2lf\n", compoundInterest);

    return 0;
}

Sample Output

Enter Principal Amount: 1000
Enter Annual Interest Rate (in percentage): 5
Enter Time Period (in years): 2
Enter Compounding Frequency (1 for Annually, 2 for Semi-Annually, 4 for Quarterly, 12 for Monthly, 365 for Daily): 4

Final Amount: 1103.81
Compound Interest: 103.81

Explanation

  • The user inputs the principal amount, annual interest rate, frequency and time period in years.
  • The program calculates the final amount using the compound interest formula, considering the frequency of compound interest provided in input.
  • It then calculates the compound interest by subtracting the principal from the final amount.
  • Finally, the results are displayed with two decimal places.

C Program to Calculate Compound Interest Using a For Loop

This program shows how instead of using the formula directly, the calculation is done iteratively using a loop. The principal amount is incrementally updated by adding the interest for each compounding period. This method is easy to understand, avoids complex mathematical functions, and allows visualization of the compounding process.

Code

#include <stdio.h>

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

    // Input details
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    printf("Enter Annual Interest Rate (in %%): ");
    scanf("%lf", &rate);
    printf("Enter Time (in years): ");
    scanf("%d", &time);
    printf("Enter Number of Compounding Periods per Year: ");
    scanf("%d", &n);

    rate = rate / 100; // Convert rate to decimal
    amount = principal;

    // Compound interest calculation using a for loop
    for (i = 0; i < n * time; i++) {
        amount = amount * (1 + rate / n);
    }

    printf("Final Amount after %d years: %.2lf\n", time, amount);

    return 0;
}

Example Input

Enter Principal Amount: 1000
Enter Annual Interest Rate (in %): 5
Enter Time (in years): 2
Enter Number of Compounding Periods per Year: 4

Example Output

Final Amount after 2 years: 1104.94

C Program to Calculate Compound Interest Using a Function

The compound interest formula is converted into a reusable function. The function takes parameters such as P, r, n, and t and computes the result using the standard formula. This method promotes modularity and reusability, making it ideal when the formula is used repeatedly with different inputs.

Code

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

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

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

    // Input details
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    printf("Enter Annual Interest Rate (in %%): ");
    scanf("%lf", &rate);
    printf("Enter Time (in years): ");
    scanf("%d", &time);
    printf("Enter Number of Compounding Periods per Year: ");
    scanf("%d", &n);

    // Call the function
    amount = calculateCompoundInterest(principal, rate, time, n);

    printf("Final Amount after %d years: %.2lf\n", time, amount);

    return 0;
}

Example Input

Enter Principal Amount: 1000
Enter Annual Interest Rate (in %): 5
Enter Time (in years): 2
Enter Number of Compounding Periods per Year: 4

Example Output

Final Amount after 2 years: 1104.94

C Program to Calculate Compound Interest Without Using the pow Function

This method implements a custom function to calculate powers iteratively, in order to avoid using the pow() function. We use this custom function to compute the final amount using the standard formula. It eliminates dependency on external libraries and is useful in situations where such libraries are unavailable.

Code

#include <stdio.h>

// Function to calculate power (x^y)
double calculatePower(double base, int exponent) {
    double result = 1;
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}

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

    // Input details
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    printf("Enter Annual Interest Rate (in %%): ");
    scanf("%lf", &rate);
    printf("Enter Time (in years): ");
    scanf("%d", &time);
    printf("Enter Number of Compounding Periods per Year: ");
    scanf("%d", &n);

    rate = rate / 100; // Convert rate to decimal
    int exponent = n * time;
    double base = (1 + rate / n);

    // Calculate compound interest without pow function
    amount = principal * calculatePower(base, exponent);

    printf("Final Amount after %d years: %.2lf\n", time, amount);

    return 0;
}

Example Input

Enter Principal Amount: 1000
Enter Annual Interest Rate (in %): 5
Enter Time (in years): 2
Enter Number of Compounding Periods per Year: 4

Example Output

Final Amount after 2 years: 1104.94

Common Questions on Compound Interest Program in C

1. How do I calculate compound interest in C?

To calculate compound interest in C, you can use the formula: A=P×(1+r(n×100))n×t

P = Principal amount

r = Annual interest rate (in %)

n = Number of times interest is compounded per year

t = Time in years 

The compound interest is then A−P.

2. Which header files are necessary for calculating compound interest in C?

You need to include stdio.h for input and output functions and math.h for mathematical functions like pow().

3. How can I handle different compounding frequencies (e.g., annually, semi-annually, quarterly) in my program?

You can do so by adjusting the variable n in the formula. The different values of n that may be required are listed below.

  1. Annually: n=1
  2. Semi-annually: n=2
  3. Quarterly: n=4
  4. Monthly: n=12
  5. Daily: n=365

4. Why is the pow() function used in calculating compound interest?

The pow() function calculates the power of a number, which is essential in the compound interest formula to raise the base to the exponent n×t.

5. How can I ensure accurate results for large principal amounts or interest rates?

For this you can use the double data type for variables to handle large numbers and ensure precision. Additionally, format specifiers like %lf in scanf and printf are used for double values.

6. Can I calculate compound interest without using the pow() function?

Yes, you can calculate it using an iterative loop to multiply the base. However, using pow() is more straightforward and efficient.

7. How do I handle user input for the principal, rate, time, and compounding frequency?

Use scanf() to take user input. Validate the input to handle unexpected or invalid values.

8. Is it possible to calculate both simple and compound interest in the same program?

Yes, you can calculate both by implementing their respective formulas and displaying the results accordingly.

9. How can I format the output to display the results with two decimal places?

Use the format specifier %.2lf in the printf() function to display numbers with two decimal places.

Half-yearly Compound Interest Program in C 

The formula for half yearly compound interest is as follows:

\( A = P \times \left( 1 + \frac{r}{2 \times 100} \right)^{2t} \)

where:

  • A = Final amount
  • P = Principal amount
  • r = Annual interest rate (in %)
  • t = Time in years

Here’s a C program to calculate compound interest half-yearly:

Code

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

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

    // Input values
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    
    printf("Enter Annual Interest Rate (in percentage): ");
    scanf("%lf", &rate);
    
    printf("Enter Time Period (in years): ");
    scanf("%lf", &time);

    // Compound interest calculation (half-yearly)
    amount = principal * pow((1 + (rate / (2 * 100))), (2 * time));
    compoundInterest = amount - principal;

    // Output results
    printf("\nFinal Amount: %.2lf\n", amount);
    printf("Compound Interest: %.2lf\n", compoundInterest);

    return 0;
}

Explanation

  • The user inputs the principal amount, annual interest rate, and time period in years.
  • The program calculates the final amount using the compound interest formula, considering half-yearly compound interest.
  • It then calculates the compound interest by subtracting the principal from the final amount.
  • Finally, the results are displayed with two decimal places.

Quarterly Compound Interest Program in C

For quarterly compound interest the formula is:

\( A = P \times \left( 1 + \frac{r}{4 \times 100} \right)^{4t} \)

where:

  • A = Final amount
  • P = Principal amount
  • r = Annual interest rate (in %)
  • t = Time in years

Code

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

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

    // Input values
    printf("Enter Principal Amount: ");
    scanf("%lf", &principal);
    
    printf("Enter Annual Interest Rate (in percentage): ");
    scanf("%lf", &rate);
    
    printf("Enter Time Period (in years): ");
    scanf("%lf", &time);

    // Compound interest calculation (quarterly)
    amount = principal * pow((1 + (rate / (4 * 100))), (4 * time));
    compoundInterest = amount - principal;

    // Output results
    printf("\nFinal Amount: %.2lf\n", amount);
    printf("Compound Interest: %.2lf\n", compoundInterest);

    return 0;
}

Explanation

  • The user inputs the principal amount, annual interest rate, and time period in years.
  • The program calculates the final amount using the compound interest formula, considering quarterly compound interest.
  • It then calculates the compound interest by subtracting the principal from the final amount.
  • Finally, the results are displayed with two decimal places.

Conclusion

Compound interest is a powerful concept that plays a significant role in financial planning and decision-making. It highlights the value of starting early and investing consistently to take full advantage of compounding growth. By understanding how it works and implementing it effectively, individuals can build wealth over time or better manage debt.

In this article, we explored what compound interest is, its formula, and how to calculate it in different ways using C programming. Each method (using loops, functions, or even custom logic) provides unique approaches to solving the same problem, depending on various levels of programming experience and requirements. 

Compound interest is not just a mathematical equation; it's a principle that underlines the importance of patience and disciplined growth in achieving financial goals. By learning and applying this concept, anyone can pave the way for smarter financial choices and long-term success.

Frequently Asked Questions

1. How can I write a C program for compound interest?

To calculate compound interest in C, use the formula:  Amount = Principal * (1 + Rate/100) ^ Time. Then, subtract the principal from the amount to find the compound interest.

2. What is a simple interest program in C?

In C, to calculate simple interest, use the formula: Simple Interest = (Principal * Rate * Time) / 100.

3. How can I write a simple C program?

A simple C program starts with #include <stdio.h>, then defines main(), uses printf() for output and scanf() for input, and returns 0.

4. How to calculate compound interest?

Compound interest is calculated with the formula: Amount = Principal * (1 + Rate/100) ^ Time. Subtract the principal from the amount to get the interest.

5. What is compound interest in Python?

In Python, compound interest is calculated using the formula: Amount = Principal * (1 + Rate/100) ** Time. The interest is the difference between the amount and the principal.

Read More Articles

Chat with us
Chat with us
Talk to career expert