谭浩强C语言设计习题答案(三)

发布于:2023-09-21 ⋅ 阅读:(84) ⋅ 点赞:(0)

目录

假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长了多少百分比。计算公式为p=(1+r)^n

存款利息的计算。有1000元,想存5年,可按以下5种办法存:


 

 

假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长了多少百分比。计算公式为p=(1+r)^n

要计算10年后我国国民生产总值与现在相比的增长百分比,可以使用下面的C语言程序,利用指定的年增长率和计算公式 `p = (1 + r)^n`:



#include <stdio.h>
#include <math.h>

int main() {
    double initialGDP = 1.0; // 初始国民生产总值,假设为1.0
    double annualGrowthRate = 0.09; // 年增长率为9%
    int years = 10; // 10年

    // 计算10年后的GDP
    double finalGDP = initialGDP * pow(1 + annualGrowthRate, years);

    // 计算增长百分比
    double growthPercentage = (finalGDP - initialGDP) / initialGDP * 100;

    printf("10年后我国国民生产总值与现在相比增长了 %.2lf%%\n", growthPercentage);

    return 0;
}

 

这个程序首先使用初始国民生产总值和年增长率计算了10年后的GDP,然后计算了增长百分比,最后将结果打印到屏幕上。在这个示例中,初始国民生产总值假设为1.0,年增长率为9%,您可以根据实际情况进行调整。

存款利息的计算。有1000元,想存5年,可按以下5种办法存:

(1)一次存5年期。

(2)先存2年期,到期后再将本息再存3年。

(3)先存3年期,到期后再将本息再存2年。

(4)存1年期,到期后将本息再存1年期,连续存5次。

(5)存活期存款。活期利息每一季度计算一次。

2007年12月的银行存款利息如下:

1年期定期存款利息为4.11%。

2年期定期存款利息为4.68%

3年期定期存款利息为5.4%

5年期定期存款利息为5.85%

活期存款利息为0.72%(活期存款每一季度结算一次利息)

如果r为年利率,n为存款年数,则计算本息和的公式为

1年期本息和:P=1000*(1+r)

n年期本息和:P=1000*(1+n*r)

存n次一年期的本息和:P=1000*(1+r)^n

活期存款本息和:P=1000*(1+r/4)4n

说明:1000*(1+r/4)是一个季节的ben'x

您可以使用以下C语言程序来计算每种存款方式的本息和:



#include <stdio.h>
#include <math.h>

int main() {
    double principal = 1000.0; // 初始本金为1000元
    double annualInterestRate; // 年利率
    int years; // 存款年数

    // 存款方式1:一次存5年期
    annualInterestRate = 0.0585; // 5年期年利率
    years = 5;
    double total1 = principal * pow(1 + annualInterestRate, years);

    // 存款方式2:先存2年期,再存3年期
    annualInterestRate = 0.0468; // 2年期年利率
    years = 2;
    double total2 = principal * pow(1 + annualInterestRate, years);
    annualInterestRate = 0.054; // 3年期年利率
    years = 3;
    total2 = total2 * pow(1 + annualInterestRate, years);

    // 存款方式3:先存3年期,再存2年期
    annualInterestRate = 0.054; // 3年期年利率
    years = 3;
    double total3 = principal * pow(1 + annualInterestRate, years);
    annualInterestRate = 0.0468; // 2年期年利率
    years = 2;
    total3 = total3 * pow(1 + annualInterestRate, years);

    // 存款方式4:存1年期,连续存5次
    annualInterestRate = 0.0411; // 1年期年利率
    years = 1;
    double total4 = principal;
    for (int i = 0; i < 5; i++) {
        total4 = total4 * pow(1 + annualInterestRate, years);
    }

    // 存款方式5:活期存款,每季度结算一次利息
    annualInterestRate = 0.0072; // 季度利率
    years = 5;
    double total5 = principal * pow(1 + annualInterestRate / 4, 4 * years);

    // 输出结果
    printf("存款方式1(一次存5年期)的本息和:%lf\n", total1);
    printf("存款方式2(先存2年期,再存3年期)的本息和:%lf\n", total2);
    printf("存款方式3(先存3年期,再存2年期)的本息和:%lf\n", total3);
    printf("存款方式4(存1年期,连续存5次)的本息和:%lf\n", total4);
    printf("存款方式5(活期存款)的本息和:%lf\n", total5);

    return 0;
}

 

这个程序分别计算了五种不同的存款方式的本息和,其中每种方式的年利率和存款年数都不同。程序使用了数学库中的 `pow` 函数来计算幂。最后,它将每种方式的本息和打印到屏幕上。


网站公告

今日签到

点亮在社区的每一天
去签到