编写一个能计算银行存款的小程序
时间: 2024-06-13 15:04:52 浏览: 13
以下是一个能计算银行存款的小程序,它可以根据用户输入的存款金额和存款年限计算出最终的本息和。请注意,本程序使用的是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>` 头文件。另外,程序中将输入的年利率转换为小数形式,以便进行计算。
用c语言编写一个计算银行存款的本息的程序
好的,下面是一个用C语言编写的计算银行存款本息的程序,它可以让用户输入存款金额、存款年限和年利率,然后计算出最终的本息和。
```c
#include <stdio.h>
#include <math.h>
int main() {
float principal, rate, interest, amount;
int years;
// 读取输入
printf("请输入存款金额: ");
scanf("%f", &principal);
printf("请输入存款年限: ");
scanf("%d", &years);
printf("请输入年利率(百分比): ");
scanf("%f", &rate);
// 计算本息和
interest = principal * pow((1 + rate / 100), years) - principal;
amount = principal + interest;
// 输出结果
printf("存款金额: %.2f\n", principal);
printf("存款年限: %d\n", years);
printf("年利率: %.2f%%\n", rate);
printf("利息: %.2f\n", interest);
printf("本息合计: %.2f\n", amount);
return 0;
}
```
注意,这个程序中用到了数学库里面的 `pow()` 函数,它用来计算幂次方。如果你想了解更多C语言的数学库函数,可以参考C语言标准库的文档。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)