用c怎么计算利息与本金
时间: 2024-05-22 21:12:52 浏览: 19
以下是一个简单的C程序,可以用来计算利息和本金:
#include <stdio.h>
int main() {
float principal, rate, time, interest, total;
// 获取本金、利率和时间
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter interest rate: ");
scanf("%f", &rate);
printf("Enter time in years: ");
scanf("%f", &time);
// 计算利息和总额
interest = (principal * rate * time) / 100;
total = principal + interest;
// 输出结果
printf("Interest = %.2f\n", interest);
printf("Total amount = %.2f\n", total);
return 0;
}
在上面的程序中,我们使用了以下变量:
- principal:本金
- rate:利率
- time:时间(以年为单位)
- interest:利息
- total:本金和利息的总额
程序首先要求用户输入本金、利率和时间,然后使用这些值计算利息和总额。最后,程序输出利息和总额。注意,我们在输出结果时使用了“%.2f”格式字符串,这将保留两位小数。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)