- The program first declares five variables:
principal, rate, time, interest, and periods. - It then prompts the user to enter the principal amount, rate of interest, time in years, and number of compounding periods per year using the
printf() and scanf() functions. - The formula for calculating compound interest is
A = P(1 + r/n)^(nt) - P, where A is the amount, P is the principal, r is the rate of interest, n is the number of times interest is compounded per year, and t is the time in years. The program calculates the compound interest using this formula and stores it in the interest variable. - Finally, the program prints the compound interest using the
printf() function with a precision of two decimal places. - Note that the
pow() function from the math.h library is used to calculate the power of a number.
#include <stdio.h>
#include <math.h>
int main() {
float principal, rate, time, interest;
int periods;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time in years: ");
scanf("%f", &time);
printf("Enter number of compounding periods per year: ");
scanf("%d", &periods);
interest = principal * pow((1 + (rate / periods)), (periods * time)) - principal;
printf("Compound interest = %.2f", interest);
return 0;
}
OUTPUT
Enter principal amount: 1000
Enter rate of interest: 5
Enter time in years: 2
Enter number of compounding periods per year: 12
Compound interest = 105.13