473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

incorrect output

Hello I am working a C++ program and the calculation is giving me the wrong total of 1317.2 when it should be 1167.1. Can you look at my code and algorithm to see what I am missing or doing wrong? I cant seem to figure this out. Thanks very much.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <math.h> 
  3. using namespace std; 
  4. int main() 
  5. int loan = 200000; 
  6. int y = 30; 
  7. int monthlyterm = y*12; 
  8. double i = .00575; //interest rate 
  9. double monthlypayment; 
  10.  
  11. monthlypayment = (loan*i)/(1-pow(1+i,-monthlyterm)); 
  12. cout<<"monthly payment is $"<<monthlypayment; 
  13. return 0;
  14.  
Oct 26 '06 #1
5 2605
Banfa
9,065 Expert Mod 8TB
I canconfirm that when compiled and run this code it prints $1317.2

However checking by hand that is correct for the data and algorithm in the program.

This means that either

1. You have not implemented your algorithm correctly

2. You have not got you constant values (e.g. the interest rate) correct

3. The method you have used to calculate the expected result of the program is wrong.


Since all you have posted is the program code and not any background information it is impossible to tell which one of these is the case in question.
Oct 26 '06 #2
I canconfirm that when compiled and run this code it prints $1317.2

However checking by hand that is correct for the data and algorithm in the program.

This means that either

1. You have not implemented your algorithm correctly

2. You have not got you constant values (e.g. the interest rate) correct

3. The method you have used to calculate the expected result of the program is wrong.



Since all you have posted is the program code and not any background information it is impossible to tell which one of these is the case in question.
Thanks I think 1 or 3 is my problem. If I use a mortgage calulator with 200,000 at 5.75 for 30 years it is 1,167 payment but my program gives 1317.2. How can I tell if 1 or 3 is my problem?
Oct 26 '06 #3
Banfa
9,065 Expert Mod 8TB
Right I see 2 problems which explain why you are not getting the same result.

1. Your program looks like it may be attempting compound interest but my calculations suggest that your mortgage calculator is doing simple interest.

In compound interest

Year Interest Rate = pow(1+ monthly interest rate, 12) - 1

In simple interest

Year Interest Rate = 12 x monthly interest rate

I also think that you have made the same mistake as me.

Mortgage calculators normally work in years but your calculation is in months.

I did this input to mortgage calculator

principle: $200000
term: 30
rate: 5.75%

but you are calculating in months with a monthly interest rate of 0.575%. Using simple interest this equates to 0.575x12 = 6.9%

When you put

principle: $200000
term: 30
rate: 6.9%

into a mortgage calculator it spits out the monthly paymemt of $1317.20

Which is what your program says.

So the problem was in 3, calculating the expected result :D
Oct 26 '06 #4
Right I see 2 problems which explain why you are not getting the same result.

1. Your program looks like it may be attempting compound interest but my calculations suggest that your mortgage calculator is doing simple interest.

In compound interest

Year Interest Rate = pow(1+ monthly interest rate, 12) - 1

In simple interest

Year Interest Rate = 12 x monthly interest rate

I also think that you have made the same mistake as me.

Mortgage calculators normally work in years but your calculation is in months.

I did this input to mortgage calculator

principle: $200000
term: 30
rate: 5.75%

but you are calculating in months with a monthly interest rate of 0.575%. Using simple interest this equates to 0.575x12 = 6.9%

When you put

principle: $200000
term: 30
rate: 6.9%

into a mortgage calculator it spits out the monthly paymemt of $1317.20

Which is what your program says.

So the problem was in 3, calculating the expected result :D
Thanks very very much. I have been trying to still get this to work with your guidance and can't seem to. At the risk of souding foolish, if my variables are as follows, can you help me get the algorithm right?

int loan = 200000;
int y = 30;
int monthlyterm = y*12;
double i = .00575; //interest rate
double monthlypayment;

monthlypayment = (loan*i)/(1-pow(1+i,-monthlyterm)); CAN'T FIGURE OUT !!!
Oct 26 '06 #5
Banfa
9,065 Expert Mod 8TB
Erm, did you not understand my previous post? Assuming that the interest rate you are using is the monthly rate (and it seems in the correct order of magnitude to be the monthly rate) then you program was working correctly.

Your calculation of the expect result was wrong.
Oct 26 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James | last post by:
Hi, I am making a basic DVD shop and after having produced code which displays a list of DVDs from my database, I implemented a search facility which enables the user to search by title, director...
5
by: Pat L | last post by:
I have a function that is designed to return a variable that contains concatenated values from a partinular field in the returned rows: DECLARE @output varchar(8000) SELECT @output = CASE...
1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
4
by: teddysnips | last post by:
I am trying to insert a row into a table using a stored procedure and I get the following error if I try this from QA: INSERT failed because the following SET options have incorrect settings:...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
1
by: iporter | last post by:
In the following code, the two Response.Write statements output exactly the same - I can copy and paste both into Query Analyzer, and run them fine. However, if I comment out line 3, the...
1
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please...
1
by: `Zidane Tribal | last post by:
it would appear that using the command 'crc("data")' from the String::CRC returns incorrect results (although, they are at least consistently incorrect). for example, this script..... ...
5
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Hi, What is wrong with this code ? I got this error: " Line 1: Incorrect syntax near 'sp_Collect'. " which appears at the last line when | call the cm.ExecuteScalar . The sp takes an int ID...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.