C program to calculate Compound Interest
Here’s a simple C program that prints a message on the screen:
#include
#include
int main() {
double principal, rate, time, compoundInterest;
printf("Enter the principal amount: ");
scanf("%lf", &principal);
printf("Enter the annual interest rate: ");
scanf("%lf", &rate);
printf("Enter the time in years: ");
scanf("%lf", &time);
compoundInterest = principal * pow((1 + rate/100), time);
printf("Compound interest is: %.2lf\n", compoundInterest);
return 0;
}
Output

Explanation:
- The program starts by including the
stdio.h
andmath.h
header files.stdio.h
is needed for input and output operations, whilemath.h
is needed for thepow
function used to calculate the exponentiation. - Inside the
main
function, we declare the variablesprincipal
,rate
,time
, andcompoundInterest
as doubles to store the respective values. - We then prompt the user to enter the principal amount, annual interest rate, and time in years using
printf
statements, and we read their input usingscanf
statements. - The compound interest is calculated using the formula:
compoundInterest = principal * pow((1 + rate/100), time)
. Thepow
function is used to calculate the exponentiation of(1 + rate/100)
to the power oftime
. - Finally, we use a
printf
statement to display the calculated compound interest with two decimal places.
When you run this program, it will prompt you to enter the principal amount, annual interest rate, and time in years. After you provide the inputs, it will calculate and display the compound interest.
- The program starts by including the