In this C program, we will calculate compound interest using user-provided inputs. Before learning the code part, let's understand compound interest and how to calculate it.
What is Compound Interest?
Compound interest is the interest that is added to the principal amount of an investment or loan at regular intervals. The interest is calculated not only on the initial principal amount but also on the accumulated interest from previous periods. As a result, the amount of interest earned keeps increasing with each compounding period.
Compound Interest Formula:
The formula to calculate compound interest is given by:
Compound Interest (CI) = P * (1 + R/n)^(n*T) - P
Where:
- CI is the Compound Interest.
- P is the Principal Amount (initial amount invested or borrowed).
- R is the Annual Interest Rate in percentage (as a decimal).
- T is the Time Duration in years.
- n is the number of times the interest is compounded per year.
Example:
Let's understand with one example. Suppose a person invests 10,000 rupees in a bank account that offers a 5% annual interest rate, compounded annually.
Given Data:
Principal amount (P) = 10,000 rupees Annual interest rate (R) = 5% (or 0.05 as a decimal) Time duration (T) = 3 years Number of times interest is compounded per year (n) = 1 Solution: CI = 10000 * (1 + 0.05/1)^(1*3) - 10000 = 10000 * (1.05)^3 - 10000 = 10000 * 1.157625 - 10000 = 11576.25 - 10000 = 1576.25 rupees
C program to find the Compound Interest.
// C program to calculate compound interest #include <stdio.h> #include <math.h> int main() { float principal, rate, time, compoundInterest; int n; // Input the principal amount printf("Enter the principal amount: "); scanf("%f", &principal); // Input the annual interest rate (as a decimal) printf("Enter the annual interest rate (as a decimal): "); scanf("%f", &rate); // Input the time duration in years printf("Enter the time duration in years: "); scanf("%f", &time); // Input the number of times interest is compounded per year printf("Enter the number of times interest is compounded per year: "); scanf("%d", &n); // Calculate the compound interest compoundInterest = principal * pow(1 + rate / n, n * time) - principal; // Display the calculated compound interest printf("Compound Interest: %.2f\n", compoundInterest); return 0; }
Enter the principal amount: 10000
Enter the annual interest rate (as a decimal): 0.05
Enter the time duration in years: 5
Enter the number of times interest is compounded per year: 1
Compound Interest: 2762.81
Note: In the above program, enter the interest rate in decimal format, like 5 % interest rate is equal to 5/100 = 0.05 to get the correct result.
No comments
Post a Comment