流↗
Lv 4
流↗ 發問時間: 電腦與網際網路程式設計 · 1 0 年前

C語言, 邏輯問題

我想寫個可以計算出要多少期的付款才可以把債付清 (loan balance)

首先會讓user輸入要借多少錢

然後輸入多少頭款(down payment),年利率(annual interest rate),月度付款(monthly payment)

之後要寫個Loop

直到債 = 0.00 

當債 "小於" 月度付款(monthly payment) 的時候,則只要把剩下的

"債 加上 利息" 付清

需要注意的是,例如你的月度付款是50.00,不不會所有的錢都拿去付債

一部分會先拿去付利息

然後有個特例是當月度付款 "小於" 利息的時候,則要馬上停止然後printf 這個債不可能被付清

我試著要寫個while loop

可是發現我不知道怎麼讓他停?

我試著不要用break因為以前教授有說過這不好

我也不知道我寫的有沒有辦法順利的減少債

#include <stdio.h>

#define monthInAYear 12int main ()

{

//初化

double sellingPrice; 想買東西的價錢

double downPayment; 頭款

double annualInterestRate; 年利率

double monthlyPayment; 月度付款

double loanBalance; 債 (想買的東西 減掉 頭款)

double monthlyInterestRate; 月利率 (年利率 除以 定數12 )

double interest; 利息(債 成以 月利率)

double paymentTowardLoan; 拿去付債的錢 (月度付款 減掉 利息)

int counter; 算loop幾次的東西

//跟user拿資料

printf("Enter the amount of Selling Price (Amount of Loan)\n");

scanf("%lf", &sellingPrice);

printf("Enter the amount of Down Payment\n");

scanf("%lf", &downPayment);

printf("Enter the Annual Interest Rate in decimal value \n");

scanf("%lf", &annualInterestRate);

printf("Enter the Payment size (Your Monthly Payment)\n");

scanf("%lf", &monthlyPayment);//不太懂這些是不要放在Loop裡面?

loanBalance = sellingPrice - downPayment;

monthlyInterestRate = annualInterestRate / monthInAYear;

interest = loanBalance * monthlyInterestRate;

paymentTowardLoan = monthlyPayment - interest;

//Loop 直到 債 = 0.00

counter = 0;

while( loanBalance > 0 ) 當債 "大於" 0

{

if ( monthlyPayment < interest ) 當月度付款 "小於" 利息

{這是我寫來停止Loop的

不過我只會prinft訊息,並不知怎麼停Loop

printf("Your loan will not be paid off! Please enter a larger amount of Monthly Payment");

}

else試著把債付掉

{

counter++; 債 = 債 減掉 拿去負債的錢

loanBalance = loanBalance - paymentTowardLoan;

}

}

if( loanBalance < monthlyPayment ) 當剩下的債 小於 月度付款

{ 月度付款 = 債 + 利息

monthlyPayment = loanBalance + interest;

}

return 0;

}

謝謝

2 個解答

評分
  • 1 0 年前
    最佳解答

    #include <stdio.h>

    #define monthInAYear 12

    int main ()

    {

    //初化

    double sellingPrice; //想買東西的價錢

    double downPayment; //頭款

    double annualInterestRate; //年利率

    double monthlyPayment; //月度付款

    double loanBalance; //債 (想買的東西 減掉 頭款)

    double monthlyInterestRate; //月利率 (年利率 除以 定數12 )

    double interest; //利息(債 成以 月利率)

    double paymentTowardLoan; //拿去付債的錢 (月度付款 減掉 利息)

    int counter; //算loop幾次的東西

    //跟user拿資料

    printf("Enter the amount of Selling Price (Amount of Loan): ");

    scanf("%lf", &sellingPrice);

    printf("Enter the amount of Down Payment: ");

    scanf("%lf", &downPayment);

    loanBalance = sellingPrice - downPayment;

    if (loanBalance > 0) //頭期款<貸款 才需分期流程

    {

    printf("Enter the Annual Interest Rate in decimal value(%%): ");

    scanf("%lf", &annualInterestRate);

    printf("Enter the Payment size (Your Monthly Payment): ");

    scanf("%lf", &monthlyPayment);

    monthlyInterestRate = annualInterestRate / monthInAYear / 100.;

    interest = loanBalance * monthlyInterestRate;

    while ( monthlyPayment < interest ) //當 月付款 < 首期利息

    {

    printf("Your loan will not be paid off! Please enter a larger amount of Monthly Payment\n");

    printf("Enter the Payment size (Your Monthly Payment): ");

    scanf("%lf", &monthlyPayment);

    }

    }

    counter = 0;

    printf("%4s%10s%10s%10s%10s\n", "期別", "債額", "月付額", "利息", "償債");

    while( loanBalance > 0 )

    {

    interest = (double)(int)(loanBalance * monthlyInterestRate+ .5);

    paymentTowardLoan = monthlyPayment - interest;

    counter++;

    if (loanBalance < paymentTowardLoan)

    {

    monthlyPayment = loanBalance + interest;

    paymentTowardLoan = loanBalance;

    }

    printf("%4d%10.0f%10.0f%10.0f%10.0f\n",

    counter,loanBalance,monthlyPayment,interest,paymentTowardLoan);

    loanBalance -= paymentTowardLoan; //債 = 債 減掉 拿去負債的錢

    }

    system("pause");

    return 0;

    }

  • 匿名使用者
    7 年前

    專辦:

    房屋貸款、民間二胎代償、代墊、土地貸款、民間二胎借款、

    汽車貸款、中古車貸款、房屋轉增貸、轉貸降息、信用貸款

    歡迎你的加入:LineID: hot777

    銀行貸款資訊 - 免費諮詢評估 - ( 0986 )-( 377 )-( 776 )

    YES貸款理財,提供您提供你最新理債及房屋貸款資訊建議,讓你生活免煩惱。

    - -歡迎同業配合- -- -歡迎同業配合- -

    歡迎同業配合,若有打擾之處,敬請見諒!

還有問題?馬上發問,尋求解答。