📅  最后修改于: 2023-12-03 15:36:28.027000             🧑  作者: Mango
本文将介绍一个使用 for 循环的年利率计算器 C++ 程序。该程序可以计算多年后的投资回报,并可以根据不同的年利率进行计算。
该程序的设计思路如下:
以下是程序源代码实现:
#include <iostream>
using namespace std;
int main() {
double investment, annual_rate;
int years;
cout << "Enter your initial investment: ";
cin >> investment;
cout << "Enter the number of years you plan to invest: ";
cin >> years;
cout << "Enter the annual interest rate: ";
cin >> annual_rate;
for (int i = 1; i <= years; i++) {
investment += (investment * annual_rate);
cout << "Year " << i << ": " << investment << endl;
}
return 0;
}
下面是该程序的使用示例:
Enter your initial investment: 10000
Enter the number of years you plan to invest: 5
Enter the annual interest rate: 0.05
Year 1: 10500
Year 2: 11025
Year 3: 11576.3
Year 4: 12155.1
Year 5: 12762.9
该程序使用 for 循环实现了一个简单的年利率计算器,可以根据用户输入的投资金额、投资年数和年利率进行计算,并输出结果。