程序主要为等额本息与等额本金两种贷款方式的对比
使用了文件流,结果均存在文件”SavedLoan.txt”里

图片略多,这次会贴到代码后面

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

//AC    等额本金 Average Capital
//ACPI  等额本息 Average Capital Plus Interest
float fLoanSum;//总需要贷款金额
float fMonthIRate;//月利率(年利率求得)
float fACPI_MonthPay;//ACPI月付
float fACPI_PaidI = 0;//ACPI已经付清的金额
float fAC_PaidP = 0;//AC已经付清的金额
float fACPI_Sum = 0;//ACPI贷款还款的本息和
float fAC_Sum = 0;//AC贷款还款的本息和
int iMonth, i;

int main()
{
    FILE *FileStream;
    float fMonthI, fMonthP;//月息和本金

    FileStream = fopen("LoanSaved.txt", "w+");

    puts("请输入:\n贷款总额(元)\t贷款年限\t贷款利率%:");
    scanf("%f %d %f", &fLoanSum, &iMonth, &fMonthIRate);
    fMonthIRate /= 12;//求得月利率
    fMonthIRate /= 100;//去掉%,直接算得月利率:x%的值
    iMonth *= 12;

    //等额本息
    fACPI_MonthPay = fLoanSum*fMonthIRate*pow(1 + fMonthIRate, iMonth) / (pow(1 + fMonthIRate, iMonth) - 1); //每月本息和
    fputs("ACPI:\n", FileStream);
    for (i = 1; i <= iMonth; i++)
    {
        fMonthI = (fLoanSum - fACPI_PaidI)*fMonthIRate; //每月利息
        fACPI_Sum += fMonthI;                           //纳入还款总额

        fMonthP = fACPI_MonthPay - fMonthI;             //每月本金
        fACPI_PaidI += fMonthP;                         //已付本金

        fprintf(FileStream, "   Month[%d]:Sum:%.2f  MonthInterest:%.2f  MonthPrincipal:%.2f\n", i, fMonthI + fMonthP, fMonthI, fMonthP);
    }

    //等额本金
    fMonthP = fLoanSum / iMonth;                        //每月本金

    fputs("AC:\n", FileStream);
    for (i = 1; i <= iMonth; i++)
    {
        fMonthI = (fLoanSum - fAC_PaidP)*fMonthIRate;   //月利息
        fAC_Sum += fMonthI;                             //计入还款额

        fAC_PaidP += fMonthP;                           //已付本金
        fprintf(FileStream, "   Month[%d]:Sum:%.2f  MonthInterest:%.2f  MonthPrincipal:%.2f\n", i, fMonthP + fMonthI, fMonthI, fMonthP);
    }
    fprintf(FileStream, "InterestSum:\n\tACPI_InterestSum:%.2f\tAC_InterestSum:%.2f\nSUM:\n\tACPI_Sum:%.2f\tAC_Sum:%.2f", fACPI_Sum, fAC_Sum, fACPI_Sum + fLoanSum, fAC_Sum + fLoanSum);

    fclose(FileStream);
    system("pause");
    return 0;
}

运行图:

等额本金 java 等额本金计算器_#include

等额本息的每月还款情况:

等额本金 java 等额本金计算器_等额本金 java_02

等额本金的每月还款情况:

等额本金 java 等额本金计算器_c语言_03

两种贷款方式的总利息和本息和:(单位:元)

等额本金 java 等额本金计算器_c语言_04