• Task 3: Write a function to calculate the (compound) interest on an initial investment of E100 at an annual interest rate of 5% by using a loop structure. Do not derive or use the compound interest formula.
The idea is to use a loop structure to add the compound interest to the present balance. You should assume that interest is paid annually and print the balance every year for 10 years. Modify your code so that the interest rate is entered from the command line. Make sure that your program issues an error if the interest rate is not a real number in the range 0≤r≤100.
#include <iostream>
void investor_profit(int interest)
{
double amount = 100.0;
for (int i = 0; i != 10; ++i)
{
amount += amount * interest / 100;
std::cout << "Year " << i + 1 << ", your currect amount of investment is amount " << amount << std::endl;
}
}
int main()
{
double interest_rate;
std::cout << "Enter interest rate" << std::endl;
std::cin >> interest_rate;
while (!std::cin.good() || interest_rate <= 0.0 || interest_rate >= 100.0)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter interest rate correctly " << std::endl;
std::cin >> interest_rate;
}
investor_profit(interest_rate);
return 0;
}
Comments
Thank you So much
Leave a comment