2-4:计算税金
编写⼀个程序,要求⽤户输⼊⼀个美元数量,然后显示出增加 5% 税率后的相应⾦额。
Enter an amount: 100.00
with tax added: $105.00
输⼊的⾦额为⼤于 0 的两位数浮点数。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x = 0.0f, y;
scanf("%f", &x);
y = (1 + 0.05) * x;
printf("y=%f\n", y);
return 0;
}
2-7:计算账单
编写一个程序,要求用户输出一个美元数量,然后显示出如何用最少20美元、10美元、5美元和1美元来付款
Enter a dollar amount: 93
$20 bills: 4
$10 bills: 1
$5 bills: 0
$1 bills: 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, d, e, f, g, h, i;
a = 20;
b = 10;
c = 5;
d = 1;
scanf("%d\n", &e);
f = e / a;
g = (e - f * a) / b;
h = (e - a * f - g * b) / c;
i = (e - a * f - b * g - c * h) / d;
printf("%d,%d,%d,%d", f, g, h, i);
return 0;
}
2-8:还贷计算
编程计算第一、第二、第三个月还贷后剩余的贷款金额
Enter amout of loan: 20000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66
Balance remaining after first payment: $19713.34
Balance remaining after second payment: $19425.25
Balance remaining after third payment: $19135.71
提示:每个月的贷款余额减去还款金额后,还需要加上贷款余额与月利率的乘积。月利率的计算方法是把用户输入的利率转换成百分数再除以12。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float loan, y_inter, m_inter, m_pay;
printf("enter amout of loan:");
scanf("%f/n", &loan);
printf("enter intere rate :");
scanf("%f/n", &y_inter);
printf("enter monthly payment:");
scanf("%f/n", &m_pay);
m_inter = y_inter / 100 / 12 + 1;
loan = (loan - m_pay) * m_inter;
printf("balance remaining after first payment:%f/n", loan);
loan = (loan - m_pay) * m_inter;
printf("balance remaining after second payment:%f/n", loan);
loan = (loan - m_pay) * m_inter;
printf("balance remaining after third payment:%f/n", loan);
return 0;
return 0;
}
3-1 日期格式转换
编写一个程序,以月/日/年即(MM/DD/YY)的格式接受用户录入的日期信息,并以年月日的格式将其显示出来
Enter a date (mm/dd/yyyy): 2/17/2011
You entered the date 20110217
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, n;
printf("Enter a date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &i, &j, &n);
printf("You entered the date %d%.2d%.2d", n, i, j);
// system("pause");
return 0;
}
3-6:分数 加法
修改如下程序( 3.2 节的 ADDFRAC.C ),使⽤户可以同时输⼊两个分数,中间⽤加号隔开:
Enter two fractions separated by a plus sign: 5/6+3/4
The sum is 38/24
最终代码
#include <stdio.h>
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
printf("Enter two fractions by a plus sign:");
scanf("%d/%d" //:: 格式字符串与给定实参不匹配
"%d/%d",
&num1, &denom1, &num2, &denom2);
result_num = num1 * denom2 + num2 * denom1;
result_denom = denom1 * denom2;
printf("The sum is %d/%d\n", result_num, result_denom);
return 0;
}