编写一个能计算银行存款的小程序
时间: 2024-06-13 11:04:52 浏览: 97
以下是一个能计算银行存款的小程序,它可以根据用户输入的存款金额和存款年限计算出最终的本息和。请注意,本程序使用的是C++语言。
```cpp
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double principal, rate, interest, amount;
int years;
cout << "请输入存款金额(元):";
cin >> principal;
cout << "请输入存款年限(年):";
cin >> years;
rate = 0.0375; // 年利率
amount = principal * pow(1 + rate, years); // 计算本息和
cout << fixed << setprecision(2) << "存款" << years << "年后的本息和为:" << amount << "元" << endl;
return 0;
}
```
相关问题
编写一个能计算银行存款的小程序。c++
好的,以下是一个简单的 C++ 程序,用于计算银行存款:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double principal, rate, time;
cout << "请输入本金:";
cin >> principal;
cout << "请输入年利率(百分数):";
cin >> rate;
rate /= 100;
cout << "请输入存款时间(年):";
cin >> time;
double interest = principal * pow(1 + rate, time) - principal;
cout << "存款利息为:" << interest << endl;
cout << "存款总额为:" << principal + interest << endl;
return 0;
}
```
程序运行后,会要求用户输入本金、年利率和存款时间,然后计算出存款利息和存款总额,并将结果输出到屏幕上。
注意,本程序使用了 `pow()` 函数计算复利,需要包含 `<cmath>` 头文件。另外,程序中将输入的年利率转换为小数形式,以便进行计算。
python编写一个能计算银行存款复利的小程序。假设目前银行年利率是1.5%,输入你的存款数,计算5年后的本金和。本金和=本金∗(1+年利率)^n n是年数。
可以使用以下Python代码实现:
```python
deposit = float(input("请输入存款金额:")) # 输入存款金额
interest_rate = 0.015 # 年利率为1.5%
years = 5 # 存款期限为5年
# 计算本金和
amount = deposit * (1 + interest_rate) ** years
# 输出结果
print("在{}年后,您的本金和为:{:.2f}元".format(years, amount))
```
运行程序后,输入存款金额,即可计算出5年后的本金和。程序会输出类似下面的结果:
```
请输入存款金额:10000
在5年后,您的本金和为:10822.50元
```
注意,这里使用了 `input` 函数来获取用户输入,需要输入一个浮点数类型的数值。使用 `format` 函数来格式化输出结果,保留两位小数。
阅读全文
相关推荐














