Compound interest is a fundamental concept in finance, representing the interest on a principal amount that continuously accumulates based on both the initial principal and the accumulated interest from previous periods. Writing a C program to compute compound interest can be immensely beneficial for financial calculations and understanding the concept’s practical implementation in programming. Let’s write a C program that computes compound interest.
Table of Contents
Understanding Compound Interest
We can calculate Compound interest by using the following formula –
A=P×(1+r/n)n×t
Where:
- ( A ) is the final amount after interest.
- ( P ) is the principal amount (initial amount).
- ( r ) is the annual interest rate (expressed as a decimal).
- ( n ) is the number of times the interest is compounded per year.
- ( t ) is the time the money is invested for, in years.
Implementation in C Program
#include <stdio.h>
#include <math.h>
// Function to calculate compound interest
double calculateCompoundInterest(double principal, double rate, int time, int compPerYear) {
double amount;
amount = principal * pow(1 + rate / compPerYear, compPerYear * time);
return amount;
}
void main() {
double principal, rate, amount;
int time, compPerYear;
// Input principal amount, rate, time, and compound frequency
printf("Enter principal amount: ");
scanf("%lf", &principal);
printf("Enter annual interest rate (in percentage): ");
scanf("%lf", &rate);
rate = rate / 100; // Convert percentage to decimal
printf("Enter time in years: ");
scanf("%d", &time);
printf("Enter compound frequency per year: ");
scanf("%d", &compPerYear);
// Calculate compound interest using the function
amount = calculateCompoundInterest(principal, rate, time, compPerYear);
// Display the result
printf("Compound interest after %d years: %.2lf\n", time, amount - principal);
}
Output
Explanation of the C Program
- The program begins by including the necessary header files (
stdio.h
for input/output andmath.h
for mathematical functions likepow()
). - A function
calculateCompoundInterest()
is defined to compute compound interest using the formula provided earlier. - Inside
main()
, user inputs are taken for principal amount, annual interest rate, time, and compound frequency per year. - The
calculateCompoundInterest()
the function is called with the provided inputs, and the calculated compound interest is displayed.
Creating a C program to calculate compound interest provides an excellent opportunity to apply mathematical formulas in programming. This program enables users to determine the compound interest accrued on an investment, demonstrating the practical implementation of finance-related concepts in C programming. Understanding and implementing compound interest computation in C can be beneficial for financial analysis, investment planning, and educational purposes.