Jump to content

I get wrong result! pls help on C++

rcarlos243
Go to solution Solved by spenser_l,
13 minutes ago, rcarlos243 said:

double payment = (( p*pow( 1+r, n ) *r ) /(pow( 1+r, n ) -1));

 

 

Spoiler

 

#include <iomanip>
#include <iostream>
using namespace std;
#include <cmath>
int main()
{
    //input values
    int p; // principle borrowed, i.e. 270000
    double rate; // anual demimal rate i.e. 5.125%
                 // the monthly rate = rate/1200
                 
                 
    // define constants
    int T = 30;  // term in years
    int n = T*12; //number of payments
    cout << "What's the amount borrowed? $";
    cin >> p;
    cin.ignore(1000, 10);
    
    cout << "What's the annual interest rate? ";
    cin >> rate;
    cin.ignore(1000, 10);
    
    double r = rate / (100*12);
    
    // mortgage
    double payment = (( p*pow( 1+r, n ) *r ) /pow( 1+r, n ) -1);
    
    cout << fixed << setprecision(2);
    // out to console
         
    cout << "\nTo borrow $" << p
         << " at the rate of " << rate <<"%"
         << " for " << T << " years,"
         << "\n your mortgage payment will be $" << payment << " per month";
}

 

 

I put amount borrowed 270,000 and I put annual interest rate at 5.125%

 

Monthly payment should be $1470.11 but I got $1152.12

 

any ideas?

 

edit: formula to calculate

yhcwcn9.png

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

Print out the value at each step of the calculation, or use a debugger, to see exactly where things go wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

You're missing some parenthesis in the formula.

can you elaborate more? I literally have no idea...

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, rcarlos243 said:

double payment = (( p*pow( 1+r, n ) *r ) /(pow( 1+r, n ) -1));

 

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, rcarlos243 said:

can you elaborate more? I literally have no idea...

 // mortgage
    double payment = (( p*pow( 1+r, n ) *r ) /pow( 1+r, n ) -1)  

 

that one? 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×