求大神用c语言编一个跟房贷有关程序

编写一个程序计算出总房贷为x (100万) 元,

(1)每年只还利息r(例如年率百分之6)列出20年,列表显示每年还掉的利息累加额。并计算20年总计利息。

(2)都不还任何钱,按利滚利算,20年 中的每年增加多少贷款,每年要还的贷款总额分别列表显示,计算20年总计利息。

(3)按6%利率计算,100万元贷款, 每年还10万,要多少年可还完贷款?列表显示每年要还余额。

(4)按6%利率计算,100万元贷款,要求20年还完。每年应还多少款?列表显示每年要还余额。

参考GPT和自己的思路:

好的,以下是针对问题的回答:

1.计算利息并显示列表如下:

#include <stdio.h>

int main()
{
    double x = 1000000;   // 总贷款额
    double r = 0.06;      // 年利率
    double interest;      // 利息
    double total_interest = 0;  // 总利息

    for(int year=1; year<=20; year++)
    {
        interest = x*r;   // 计算利息
        total_interest += interest;  // 累计总利息
        printf("第 %2d 年还款利息:%.2lf 元\n", year, interest);
        x -= interest;   // 减去本年度利息后的剩余贷款额
    }

    printf("20 年总计利息为:%.2lf 元\n", total_interest);

    return 0;
}

2.按利滚利每年增加贷款并计算利息列表如下:

#include <stdio.h>

int main()
{
    double x = 1000000;   // 总贷款额
    double r = 0.06;      // 年利率
    double interest;      // 利息
    double total_interest = 0;  // 总利息

    for(int year=1; year<=20; year++)
    {
        interest = x*r;   // 计算利息
        total_interest += interest;  // 累计总利息
        printf("第 %2d 年还款利息:%.2lf 元,需增加贷款:%.2lf 元,共计 %.2lf 元\n", year, interest, interest+x, x+interest+x);
        x += interest;   // 加上本年度利息后的剩余贷款额
    }

    printf("20 年总计利息为:%.2lf 元\n", total_interest);

    return 0;
}

3.每年还款并计算余额列表如下:

#include <stdio.h>

int main()
{
    double x = 1000000;   // 总贷款额
    double r = 0.06;      // 年利率
    double pay = 100000;  // 每年还款额
    double balance;       // 本年度还款后的余额

    int year = 1;
    while(x > 0)
    {
        double interest = x*r;   // 计算利息
        balance = x + interest - pay;   // 计算余额
        if(balance < 0) balance = 0;   // 如果贷款已还清,余额为0
        printf("第 %2d 年,本年度贷款余额:%.2lf 元\n", year, balance);
        x = balance;   // 更新贷款余额
        year++;   // 年数累加
    }

    printf("共计 %d 年还清贷款\n", year-1);

    return 0;
}

4.每年需还款列表如下:

#include <stdio.h>

int main()
{
    double x = 1000000;   // 总贷款额
    double r = 0.06;      // 年利率
    double pay;           // 每年还款额

    pay = x*r*(1+r)/((1+r)*20-1);   // 每年需还款额
    for(int year=1; year<=20; year++)
    {
        double interest = x*r;   // 计算利息
        printf("第 %2d 年应还款额:%.2lf 元,其中利息:%.2lf 元,本金:%.2lf 元\n", year, pay, interest, pay-interest);
        x -= (pay-interest);   // 更新贷款余额
    }

    return 0;
}

以上是我的回答,希望能够帮助到你。