2014-02-11 131 views
-1

我正在編寫計算貸款月度支付的程序。但它並沒有給出正確的答案。這裏是我的代碼:C++中的貸款還款計算

#include<iostream> 
#include<cmath> 

using namespace std; 

int main() 
{ 

    double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt; 
    int NumPayments; 

    cout << "Enter the loan amount (LoanAmount) --> "; 
    cin >> LoanAmount; 

    cout << "Enter the YEARLY interest rate as a percentage --> "; 
    cin >> YearlyInt; 

    cout << "Enter number of payments --> "; 
    cin >> NumPayments; 

    cout << "Loan amount: " << LoanAmount << endl; 
    cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl; 
    cout << "Number of Payments: " << NumPayments << endl; 

    MonthlyInt = YearlyInt/12; 

    Payment = MonthlyInt * pow ((1 + MonthlyInt), NumPayments)/(pow((1 + MonthlyInt), NumPayments) -1) * LoanAmount; 
    cout << "Monthly Payment: " << Payment << endl; 

    AmountPaid = Payment * 36; 

    cout << "Amount Paid Back: " << AmountPaid << endl; 
    cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl; 
    cout << "Program Over" << endl << endl << endl << endl; 
    cout << "Press Enter to end -->" << endl; 

    return 0; 
} 

程序使用這個公式:

  MonthlyInt * pow(1 + MonthlyInt, NumPayments) * LoanAmount 
Payment = --------------------------------------------------------------- 
        pow(1 + MonthlyInt, NumPayments) - 1 

這是我得到的輸出:

Enter the loan amount (LoanAmount) --> 10000 
Enter the YEARLY interest rate as a percentage --> 12 
Enter number of payments --> 36 
Loan amount: 10000 
Yearly Interest Rate: 12% 
Number of Payments: 36 
Monthly Payment: 10000 
Amount Paid Back: 360000 
Interest Paid: 350000 
Program Over 

Press Enter to end --> 
Press any key to continue . . . 

正如你所看到的,貸款金額爲明顯錯誤。我如何修復我的代碼?

+1

「沒有給出正確的答案」並不是真的告訴我們你期望它真的做什麼。你現在得到什麼,你想得到什麼? –

+0

對不起,我將來會更加清楚。 –

+0

只需編輯問題並添加信息即可。沒有必要抱歉,只需要修理東西!我敢打賭,如果你告訴我們關於真正的問題,那麼人們就會投票回來。 –

回答

0

有幾個問題:

  1. 您以百分比輸入率,從而將其轉換爲十進制數字:MonthlyInt/100.0

  2. 款項的數目應該是固定的,或者由用戶輸入。現在它首先被讀入,但是在代碼中使用了36。它應該被替換爲適當的變量。

  3. 小心整數除法。目前沒有錯誤,但爲了避免出現這種情況,如果你想確保你有浮動,請使用1.0和100.0,而不是1和100。

  4. 確保你的數學是正確的。事實上,這應該是你做的第一件事。儘管這是一個編程網站,所以它在這裏是無關緊要的。

  5. (可選)通常,變量名稱不應以大寫字母開頭。

+0

謝謝,我將MonthlyInt公式轉換爲MonthlyInt =(YearlyInt/100.0)/ 12;我剛剛看到我硬編碼36在大聲笑我會拿出來。現在工作! –

+0

我不認爲他在他的程序中執行整數除法。 –

+0

@ andrew.punnett我認爲MonthlyInt在第一次修訂中是整數,但它不是,是的,沒有...我會編輯答案。 – sashkello

0

該計劃是否應該計算複利或單利?

您的計算似乎不正確。您計算每月利率,這看起來像是單純的興趣,但是您使用pow,這表明與複利有關的事情。你應該仔細研究一下。

0
#include<iostream> 
#include<cmath> 

using namespace std; 

int main() 
{ 

double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments; 




cout << "Enter the loan amount (LoanAmount) --> "; 
cin >> LoanAmount; 

cout << "Enter the YEARLY interest rate as a percentage --> "; 
cin >> YearlyInt; 

cout << "Enter number of payments --> "; 
cin >> NumPayments; 



cout << "Loan amount: " << LoanAmount << endl; 
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl; 
cout << "Number of Payments: " << NumPayments << endl; 

MonthlyInt = (YearlyInt/100.0)/12; 

Payment = MonthlyInt * pow ((1 + MonthlyInt), NumPayments)/(pow((1 + MonthlyInt), NumPayments) -1) * LoanAmount; 
cout << "Monthly Payment: " << Payment << endl; 

AmountPaid = Payment * 36; 
cout << "Amount Paid Back: " << AmountPaid << endl; 
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl; 


cout << "Program Over" << endl << endl << endl << endl; 

cout << "Press Enter to end -->" << endl; 



return 0; 

得到它簡單地切換MonthlyInt = YearlyInt/12;至MonthlyInt =(每年/ 100.0)/ 12;

1

這裏有一個程序,它正確地計算支付假設如下:

  • 年利息計算複利的月息。
  • 有沒有費用的貸款。
  • 償還款項在貸款發放一個月後開始。
  • 每月付款金額不變。
  • 沒有錯過每月付款。

#include<iostream> 
#include<cmath> 

using namespace std; 

int main() 
{ 
    double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt; 

    int NumPayments; 

    cout << "Enter the loan amount (LoanAmount) --> "; 
    cin >> LoanAmount; 

    cout << "Enter the YEARLY interest rate as a percentage --> "; 
    cin >> YearlyInt; 

    cout << "Enter number of monthly payments --> "; 
    cin >> NumPayments; 

    cout << "Loan amount: " << LoanAmount << endl; 
    cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl; 
    cout << "Number of Monthly Payments: " << NumPayments << endl; 

    MonthlyInt = pow(1 + YearlyInt/100, 1.0/12); 

    Payment = LoanAmount * pow(MonthlyInt, NumPayments) * 
          (MonthlyInt - 1)/
          (pow(MonthlyInt, NumPayments) - 1); 

    cout << "Monthly Payment: " << Payment << endl; 

    AmountPaid = Payment * NumPayments; 
    cout << "Amount Paid Back: " << AmountPaid << endl; 
    cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl; 

    cout << "Program Over" << endl << endl << endl << endl; 

    return 0; 
} 
1

步驟1:MonthlyInt做的,因爲複利的影響不等於YearlyInt/12。在一個較小的週期率和一個較大週期的等價率之間轉換的一般公式是:(1 + r)^ n = 1 + R.所以在這種情況下r = MonthlyInt和R = YearlyInt。因此,企業的首要任務是從改變 :

MonthlyInt = YearlyInt/12; 

到:

MonthlyInt = pow ((1.0 + YearlyInt) , (1.0/NumPayments)) - 1.0; // note decimals! 

第2步:添加打印MonthlyInt這樣就可以驗證計算一行。 :)

步驟3:更改AmountPaid = Payment * 36;AmountPaid = Payment * NumPayments;

第4步:可選,加上美元符號和清理小數。

我們必須添加標頭#include<iomanip>,然後設置cout << setprecision(n) << fixed << whateverVariable的小數位數,其中n等於您想要的小數位數。

修改後的代碼:

#include<iostream> 
#include<cmath> 
#include<iomanip> 

using namespace std; 

int main() 
{ 
    double YearlyInt = -1, LoanAmount = -1, Payment = -1, AmountPaid = -1, MonthlyInt = -1; 

    int NumPayments; 

    cout << "Enter the loan amount (LoanAmount) --> "; 
    cin >> LoanAmount; 

    cout << "Enter the YEARLY interest rate as a decimal number (e.g. 3.25% as .0325) --> "; 
    cin >> YearlyInt; 

    cout << "Enter number of payments --> "; 
    cin >> NumPayments; 

    cout << "Loan amount: $" << setprecision(2) << fixed << LoanAmount << endl; 
    cout << "Yearly Interest Rate: " << setprecision(3) << YearlyInt * 100 << "%" << endl; 
    cout << "Number of Payments: " << NumPayments << endl; 

    MonthlyInt = pow ((1.0 + YearlyInt) , (1.0/NumPayments)) - 1.0; 

    cout << "MonthlyInt: " << MonthlyInt*100 << "%" << endl; 

    Payment = MonthlyInt * pow ((1 + MonthlyInt), NumPayments)/(pow((1 + MonthlyInt), NumPayments) -1) * LoanAmount; 
    cout << "Monthly Payment: $" << setprecision(2) << Payment << endl; 

    AmountPaid = Payment * NumPayments; 
    cout << "Amount Paid Back: $" << AmountPaid << endl; 
    cout << "Interest Paid: $" << (AmountPaid - LoanAmount) << endl; 


    cout << "Program Over" << endl << endl << endl << endl; 

    cout << "Press Enter to end -->" << endl; 

    return 0; 
} 

假設:貸款有YearlyInt無費APR要按月計算複利,每月支付,施加於同月的最後一天的首付款中貸款的發起和所有「準時」付款(無論是由貸款人定義的)如同在適用期限的最後一天支付一樣。

0
#include<iostream> 
#include<cmath> 

using namespace std; 

int main() 
{ 

    double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments; 




    cout << "Enter the loan amount (LoanAmount) --> "; 
    cin >> LoanAmount; 

    cout << "Enter the YEARLY interest rate as a percentage --> "; 
    cin >> YearlyInt; 

    cout << "Enter number of payments --> "; 
    cin >> NumPayments; 



    cout << "Loan amount: " << LoanAmount << endl; 
    cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl; 
    cout << "Number of Payments: " << NumPayments << endl; 

    MonthlyInt = (YearlyInt/100.0)/12; 

    Payment = MonthlyInt * pow ((1 + MonthlyInt), NumPayments)/(pow((1 + MonthlyInt), NumPayments) -1) * LoanAmount; 
    cout << "Monthly Payment: " << Payment << endl; 


    //correction to Amount paid Value 
    AmountPaid = Payment * NumPayments; 
    cout << "Amount Paid Back: " << AmountPaid << endl; 
    cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl; 


    cout << "Program Over" << endl << endl << endl << endl; 

    cout << "Press Enter to end -->" << endl; 



    return 0; 
} 
+0

歡迎來到Stack Overflow!儘管這段代碼可以解決這個問題,但[包括一個解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! –