存款利率计算器c语言
1.
存款利率计算器是一个利用C语言编写的程序,用户可以输入银行定期存款的年利率、存款期限和存款本金额度,然后程序会根据用户输入的数据计算并输出存款期限结束后的本利之和。该程序中所有浮点数的数据类型均为double类型,用户可以通过函数scanf()从键盘输入数据。
2. 从键盘输入变量值
用户需要利用程序从键盘上输入两个变量a和b的值,然后计算a+|b|。
#includeint main() {
int a, b, sum
printf("Enter the value of a: ")
scanf("%d", &a)
printf("Enter the value of b: ")
scanf("%d", &b)
sum = a + abs(b)
printf("Sum of a and |b| is %d\n", sum)
return 0
在以上代码中,用户需要使用指令#include 引入标准输入输出函数库。然后,用户可以使用scanf()函数从键盘上输入变量a和b的值,并使用abs()函数计算|b|的绝对值。用户可以使用printf()函数将计算结果输出到屏幕上。
3. 计算定期存款的利息和本息合计
用户可以输入存款金额并选择存款种类,程序将根据选择的存款种类确定利率和存期后进行计算,最终输出利息和本息合计。
#includeint main() {
double principal, rate, interest, total
int period
printf("Enter the deposit amount: ")
scanf("%lf", &principal)
printf("Choose the deposit period:\n")
printf("1. Three months\n2. Six months\n3. One year\n")
printf("4. Two years\n5. Three years\n6. Five years\n")
scanf("%d", &period)
switch(period) {
case 1:
rate = 0.02
break
case 2:
rate = 0.025
break
case 3:
rate = 0.03
break
case 4:
rate = 0.035
break
case 5:
rate = 0.04
break
case 6:
rate = 0.045
break
default:
printf("Invalid choice.\n")
return 1
}
interest = principal * rate
total = principal + interest
printf("Interest: %.2lf\n", interest)
printf("Total: %.2lf\n", total)
return 0
在以上代码中,用户需要使用指令#include 引入标准输入输出函数库。然后,用户可以使用scanf()函数从键盘上输入存款金额,并使用printf()函数将存款期限选项输出到屏幕上。之后,用户可以使用scanf()函数再次从键盘上输入存款期限,并使用switch语句确定利率。程序会根据输入的存款金额和确定的利率计算存款的利息和本息合计,并使用printf()函数将结果输出到屏幕上。
4. 计算存款到期后本金与利息的合计
这个程序可以根据用户输入的存款金额、存款年限和年利率,计算存款到期时的本金与利息的合计。
#includeint main() {
double capital, rate, deposit
int year
printf("Enter the capital: ")
scanf("%lf", &capital)
printf("Enter the rate: ")
scanf("%lf", &rate)
printf("Enter the number of years: ")
scanf("%d", &year)
deposit = capital + (capital * rate * year)
printf("Total deposit after %d years: %.2lf\n", year, deposit)
return 0
在以上代码中,用户需要使用指令#include 引入标准输入输出函数库。然后,用户可以使用scanf()函数从键盘上输入存款金额、存款年限和年利率,并使用printf()函数将计算结果输出到屏幕上。
- 上一篇:建行理财产品哪个最安全