C语言计算不同期限利率不同的存款
时间: 2024-05-13 15:20:47 浏览: 13
下面是一个简单的C语言程序,用于计算不同期限利率不同的存款:
```
#include <stdio.h>
int main() {
int months; // 存款期限,以月为单位
double rate; // 存款利率,以年为单位
double amount; // 存款金额
printf("请输入存款期限(以月为单位):");
scanf("%d", &months);
printf("请输入存款利率(以年为单位):");
scanf("%lf", &rate);
printf("请输入存款金额:");
scanf("%lf", &amount);
double interest = amount * rate / 12 * months / 100; // 计算利息
double total = amount + interest; // 计算存款总额
printf("存款期限:%d个月\n", months);
printf("存款利率:%.2f%%\n", rate);
printf("存款金额:%.2f元\n", amount);
printf("利息:%.2f元\n", interest);
printf("存款总额:%.2f元\n", total);
return 0;
}
```
该程序通过用户输入存款期限、存款利率和存款金额,计算存款所得的利息和存款总额。利率是以年为单位给出的,因此需要将其转换为月利率。存款期限也是以月为单位给出的,因此需要将其转换为年。
在实际应用中,存款利率可能会根据不同期限的存款而有所不同。可以通过数组来存储不同期限的利率,然后根据用户输入的存款期限来选择相应的利率进行计算。以下是一个示例代码:
```
#include <stdio.h>
int main() {
int months; // 存款期限,以月为单位
double rates[] = {1.5, 1.8, 2.0, 2.5}; // 不同期限的存款利率,以年为单位
double amount; // 存款金额
printf("请输入存款期限(以月为单位):");
scanf("%d", &months);
double rate;
if (months <= 12) {
rate = rates[0] / 12;
} else if (months <= 24) {
rate = rates[1] / 12;
} else if (months <= 36) {
rate = rates[2] / 12;
} else {
rate = rates[3] / 12;
}
printf("存款利率:%.2f%%\n", rate * 100);
printf("请输入存款金额:");
scanf("%lf", &amount);
double interest = amount * rate * months / 100; // 计算利息
double total = amount + interest; // 计算存款总额
printf("存款期限:%d个月\n", months);
printf("存款金额:%.2f元\n", amount);
printf("利息:%.2f元\n", interest);
printf("存款总额:%.2f元\n", total);
return 0;
}
```
该程序把不同期限的利率存储在一个数组中,根据用户输入的存款期限选择相应的利率进行计算。如果用户输入的存款期限超过了最长期限,可以在程序中添加相应的处理逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)