c how to program 习题3.19

发布于2020-04-29 842 热度

运行结果:

C程序代码:

/* c how to program 习题3.19 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    //分别定义变量
    double interest, principal, rate;
    int days;

    //获取第一次的贷款金额
    printf("Enter Loan principal (-1 to end): ");
    scanf("%lf", &principal);

    //进入循环,直到输入-1结束
    while (principal != -1.0){
        //获取年利率
        printf("Enter interest rate: ");
        scanf("%lf", &rate);

        //获取贷款天数
        printf("Enter term of the loan in days: ");
        scanf("%d", &days);

        //计算利息并输出
        interest = principal * rate * days / 365.0;
        printf("The interest charge is $%.2lf\n\n", interest);

        //获取下一次的贷款金额
        printf("Enter Loan principal (-1 to end): ");
        scanf("%lf", &principal);
    }
    
    return 0;
    
}

记录工作,记录学习,分享知识,分享经验……