c语言计算银行本息和存款
时间: 2024-10-27 13:05:32 浏览: 80
在C语言中,计算银行本息和通常涉及到复利计算,特别是对于定期存款。下面是一个简单的例子,假设年利率、计息方式(比如简单利息或复利)、存款期数和每次存款金额都是固定的:
```c
#include <stdio.h>
// 定义函数,输入本金、年利率、期限以及计息次数,返回总利息和总额
double calculate_total(double principal, double annual_rate, int years, int compounding_times) {
double interest = principal * (annual_rate / compounding_times);
double total_after_years = principal + interest;
if (compounding_times > 1) { // 如果是复利,需要多次累加
for (int i = 1; i < years; i++) {
interest = total_after_years * (annual_rate / compounding_times);
total_after_years += interest;
}
} else { // 简单利息
for (int i = 0; i < years; i++) {
interest *= (1 + annual_rate);
total_after_years += interest;
}
}
return total_after_years;
}
int main() {
double deposit_amount = 10000.0; // 存款本金
double rate = 0.05; // 年利率,如5%
int years = 3; // 存款年限
int compounding_times_per_year = 1; // 每年计息一次
double total_with_interest = calculate_total(deposit_amount, rate, years, compounding_times_per_year);
printf("存款 %lf 元,年利率 %.2f%%,存款 %d 年后的本息和为 %.2f 元.\n", deposit_amount, rate * 100, years, total_with_interest);
return 0;
}
```
阅读全文