C Program – Find Compound Interest - Using pow() & Loop

Compound Interest Using pow()

#include <stdio.h>
#include <math.h>
int main() {
    double principal, rate, time, amount, interest;
    printf("Enter principal amount: ");
    scanf("%lf", &principal);
    printf("Enter annual interest rate (in %%): ");
    scanf("%lf", &rate);
    printf("Enter time (in years): ");
    scanf("%lf", &time);
    amount = principal * pow((1 + rate/100), time);
    interest = amount - principal;
    printf("Amount = %.2lf\n", amount);
    printf("Compound Interest = %.2lf\n", interest);
    return 0;
}
Enter principal amount: 1000
Enter annual interest rate (in %): 5
Enter time (in years): 2
Amount = 1102.50
Compound Interest = 102.50
        

The compound interest formula calculates the accumulated amount by raising (1 + rate/100) to the power of the number of years and multiplying it by the principal. Subtracting the original principal yields the interest earned.

Compound Interest Using a Loop

#include <stdio.h>
int main() {
    double principal, rate, time, amount;
    int i;
    printf("Enter principal amount: ");
    scanf("%lf", &principal);
    printf("Enter annual interest rate (in %%): ");
    scanf("%lf", &rate);
    printf("Enter time (in years): ");
    scanf("%lf", &time);
    amount = principal;
    for (i = 1; i <= time; i++) {
        amount = amount + (amount * rate / 100);
    }
    printf("Amount = %.2lf\n", amount);
    printf("Compound Interest = %.2lf\n", amount - principal);
    return 0;
}
Enter principal amount: 2000
Enter annual interest rate (in %): 3
Enter time (in years): 3
Amount = 2185.45
Compound Interest = 185.45
        

Instead of using pow(), this method iteratively updates the amount each year by adding the interest earned in that period. After all periods, the difference between the final amount and the principal gives the compound interest.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...