Calculate compound interest program for the interest rates 5%, 6%, 7%, 8%, 9% and 10%. Use a for statement. - C++ Statement

C++ examples for Statement:for

Description

Calculate compound interest program for the interest rates 5%, 6%, 7%, 8%, 9% and 10%. Use a for statement.

Demo Code

#include <cmath>
#include <iomanip>
#include <iostream>

int main(int argc, const char *argv[]) {
    double amount;
    double principal = 1000.0f;

    std::cout << "Year" << std::setw(8) << "Rate" << std::setw(21)
              << "Amount on deposit" << std::endl;

    std::cout << std::fixed << std::setprecision(2);

    for (int rate = 5; rate <= 10; rate++) {
        for (int year = 0; year <= 10; ++year) {
            // set correct rate / 100
            amount = principal * pow(1.0f + rate / 100.0f, year);
            std::cout << std::setw(4) << year << std::setw(8) << rate / 100.0f
                      << std::setw(15) << amount << std::endl;
        }/*  w w  w  .  j  a  va  2 s  . c o m*/
        std::cout << std::endl;
    }
    return 0;
}

Result


Related Tutorials