C语言程序设计:现代方法 第二章 答案

// 1
#include <stdio.h>

int main() {
    printf(
            "       *\n"
            "      *\n"
            "     *\n"
            "*   *\n"
            " * *\n"
            "  *"
            );
}
// 2 - 3
#include <stdio.h>

#define PI 3.1415926 // 圆周率

int main() {
    double radius, // 半径
           volume; // 体积

    printf("输入半径:");
    scanf("%lf", &radius);

    // 计算球的体积
    volume = 4.0 / 3.0 * PI * radius * radius * radius;

    printf("体积是:%f", volume);

    return 0;
}
// 4
#include <stdio.h>

#define TAX_RATE 0.05f // 定义税率为 0.05(即 5%)

int main(void)
{
    float amount;       // 存储用户输入的金额
    float finalAmount;  // 存储增加税率后的最终金额

    printf("Enter an amount: ");
    scanf("%f", &amount); // 获取用户输入的金额,并存储到变量 amount 中

    finalAmount = amount * (1 + TAX_RATE); // 计算增加税率后的金额,通过将原始金额乘以 (1 + TAX_RATE) 实现

    printf("With tax added: $%.2f\n", finalAmount); // 将增加税率后的最终金额输出到屏幕上,保留两位小数

    return 0;
}

// 5 - 6
#include <stdio.h>

int main(void)
{
    int amount;
    int twenties, tens, fives, ones;

    printf("请输入付款金额:");
    scanf("%d", &amount);

    /* 计算 20 美元钞票的数量 */
    twenties = amount / 20;
    /* 将 20 美元的总金额从付款金额中减去 */
    amount -= twenties * 20;

    /* 计算 10 美元钞票的数量 */
    tens = amount / 10;
    /* 将 10 美元的总金额从付款金额中减去 */
    amount -= tens * 10;

    /* 计算 5 美元钞票的数量 */
    fives = amount / 5;
    /* 将 5 美元的总金额从付款金额中减去 */
    amount -= fives * 5;

    /* 计算 1 美元钞票的数量 */
    ones = amount;

    printf("$20 bills: %d\n$10 bills: %d\n $5 bills: %d\n $1 bills: %d\n",
           twenties, tens, fives, ones);

    return 0;
}
// 8
#include <stdio.h>



int main() {
    double loanAmount, // 贷款金额
    interestRate, // 利息率
    monthlyPayment, // 每月付款
    balance; // 贷款余额

    printf("请输入贷款金额(美元):");
    scanf("%lf", &loanAmount);

    printf("请输入年利率(百分数):");
    scanf("%lf", &interestRate);

    printf("请输入每月还款额(美元):");
    scanf("%lf", &monthlyPayment);

    interestRate /= 100; // 转成小数

    // 计算每月利率
    double monthlyInterestRate = interestRate / 12;

    // 每月还款后的余额
    balance = loanAmount - monthlyPayment;
    balance += balance * monthlyInterestRate;
    printf("第一个月还款后的余额为 $%.2f\n", balance);
    balance -= monthlyPayment;
    balance += balance * monthlyInterestRate;
    printf("第二个月还款后的余额为 $%.2f\n", balance);
    balance -= monthlyPayment;
    balance += balance * monthlyInterestRate;
    printf("第三个月还款后的余额为 $%.2f\n", balance);

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值