C program to calculate Compound Interest

Here’s a simple C program that prints a message on the screen:

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

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;

C Program For Compound Interest
  • Explanation:

    • The program starts by including the stdio.h and math.h header files. stdio.h is needed for input and output operations, while math.h is needed for the pow function used to calculate the exponentiation.
    • Inside the main function, we declare the variables principal, rate, time, and compoundInterest 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 using scanf statements.
    • The compound interest is calculated using the formula: compoundInterest = principal * pow((1 + rate/100), time). The pow function is used to calculate the exponentiation of (1 + rate/100) to the power of time.
    • 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.