Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to calculate annually compound interest in C

2 Answers

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
0 votes
#include <stdio.h>
#include <math.h>

/*
 * Calculate the future value of an investment using
 * annual compound interest.
 *
 * Formula:
 *     A = P * (1 + r)^t
 *
 * Where:
 *     A = final amount
 *     P = principal (initial investment)
 *     r = annual interest rate as a decimal
 *     t = number of years
 *
 * Parameters:
 *     principal    - initial amount invested
 *     rate_percent - annual interest rate (e.g. 3.5 for 3.5%)
 *     years        - investment duration in years
 *
 * Returns:
 *     The future value after annual compounding.
 */
double compound_interest(double principal,
                         double rate_percent,
                         unsigned int years)
{
    const double rate = rate_percent / 100.0;

    return principal * pow(1.0 + rate, years);
}

int main(void)
{
    /* Input data */
    const double principal = 100000.0;
    const double rate = 3.5;
    const unsigned int years = 5;

    /* Perform calculation */
    const double final_amount =
        compound_interest(principal, rate, years);

    const double interest_earned =
        final_amount - principal;

    /* Display results */
    printf("Principal: $%.2f\n", principal);
    printf("Annual Rate: %.2f%%\n", rate);
    printf("Years: %u\n", years);
    printf("Final Amount: $%.2f\n", final_amount);
    printf("Interest Earned: $%.2f\n", interest_earned);

    return 0;
}


/*
run:

Principal: $100000.00
Annual Rate: 3.50%
Years: 5
Final Amount: $118768.63
Interest Earned: $18768.63

*/

 



answered 2 days ago by avibootz

Related questions

1 answer 128 views
1 answer 132 views
1 answer 136 views
1 answer 142 views
1 answer 142 views
1 answer 152 views
...