How to calculate annually compound interest in C

1 Answer

0 votes
#include <stdio.h>
#include <math.h> // pow()

// Function to calculate compound interest
double calculateCompoundInterest(double principal, double rate, double years) {
    if (principal < 0 || rate < 0 || years < 0) {
        printf("Error: Principal, rate, and years must be non-negative values.\n");
        return -1;
    }

    // Calculate total amount using the compound interest formula
    double amount = principal * pow((1 + rate / 100), years);
    
    // Return compound interest
    return amount - principal;
}

int main() {
    double principal = 100000, rate = 3.5, years = 5;

    // Call the function to calculate compound interest
    double compoundInterest = calculateCompoundInterest(principal, rate, years);
    
    // Display the results
    printf("Principal Amount: %.2lf\n", principal);
    printf("Annual Interest Rate: %.2lf%%\n", rate);
    printf("Years: %.2lf\n", years);
    printf("Compound Interest: %.2lf\n", compoundInterest);
    printf("Total Amount: %.2lf\n", principal + compoundInterest);

    return 0;
}


    
/*
run:
   
Principal Amount: 100000.00
Annual Interest Rate: 3.50%
Years: 5.00
Compound Interest: 18768.63
Total Amount: 118768.63
   
*/

 



answered Jul 21, 2018 by avibootz
edited Aug 30, 2025 by avibootz

Related questions

1 answer 70 views
1 answer 60 views
1 answer 78 views
1 answer 72 views
1 answer 65 views
...