943,670 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 567
  • C++ RSS
Aug 12th, 2009
0

Number to a percentage

Expand Post »
Hello everyone. I am pretty lost when it comes to C++, but for class i have to create this mortgage calculator. I think i have this part of the assignment done, but i can not figure out how to convert either the last number, or the user input to a percentage.

So any help would be great!! Thanks everyone!

c++ Syntax (Toggle Plain Text)
  1.  
  2. /*
  3.  Write the program as a procedural C++ program. Calculate
  4.  and display the mortgage payment amount using the amount
  5.  of the mortgage, the term of the mortgage, and the interest
  6.  rate of the mortgage as input by the user. Allow the user to
  7.  loop back and enter new data or quit. Insert comments in the
  8.  program to document the program.
  9.  */
  10.  
  11.  
  12. #include <iostream>
  13. #include <cmath>
  14.  
  15.  
  16. using namespace std;
  17.  
  18. int main () {
  19.  
  20. double principle = 200000;
  21. double interest = .00575;
  22. int term = 30 * 12;
  23. double total;
  24.  
  25. double monthlyInterest = interest * 12;
  26.  
  27. total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term));
  28.  
  29. cout << "Enter Loan Amount: " <<endl;
  30. cin >> principle;
  31.  
  32. if (principle <= 0){
  33. cout << "Enter valid input: ";
  34. cin >> principle;
  35. }
  36.  
  37. cout << "Enter desired Interest Rate: " << endl;
  38. cin >> interest;
  39.  
  40. if (interest <= 0) {
  41. cout << "Please enter a valid Interest Rate";
  42. cin >> interest;
  43.  
  44. }
  45.  
  46. cout << "Your priniple is " << principle << endl;
  47. cout << "Your Interste rate is: " << interest << endl;
  48. cout << "Term Length: " << term << " Months" << endl;
  49. cout << "Your total monthly payment is: " << total << endl;
  50.  
  51.  
  52. return 0;
  53. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 12th, 2009
0

Re: Number to a percentage

To convert decimal value to percentage you divide a value by
its max attainable value. For interest if user inputs 6 percent, the
max percent attainable is 100, so we divide 6 by 100 to get the
percentage.
Also
C++ Syntax (Toggle Plain Text)
  1. if (interest <= 0) {
  2. cout << "Please enter a valid Interest Rate";
  3. cin >> interest;
  4.  
  5. }
You might want to make that a while loop, so until the user enters
a correct value, he gets prompted.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 12th, 2009
0

Re: Number to a percentage

Thank you for the help. I dont think that i explained it well enough. I go through the program but at the end it is not giving me the correct answer. i know that the formula is correct, but i know its not right this time. (i build this program more primitive for the last assignment) I am not sure what i am doing wrong though.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 12th, 2009
0

Re: Number to a percentage

Try to explain more as it's not really clear what you are trying to ask. The program compiles and is working properly. In mathematics, a percentage is nothing except division by 100. So most of the time to convert anything to percentage, you have to divide it by 100 and put % next to it.
Reputation Points: 7
Solved Threads: 22
Junior Poster
group256 is offline Offline
182 posts
since Apr 2009
Aug 12th, 2009
0

Re: Number to a percentage

When i compile the program, it does work. The problem is i believe it was the incorrect answer. I am not sure on how to correct this. I thought that the math was correct, but i guess not. Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hollywoood is offline Offline
10 posts
since Aug 2009
Aug 12th, 2009
0

Re: Number to a percentage

do you remember the formula for this project. It should be on your
handout.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 12th, 2009
0

Re: Number to a percentage

C++ Syntax (Toggle Plain Text)
  1. double interest = .00575;
I think the monthly interest is given.

C++ Syntax (Toggle Plain Text)
  1. double monthlyInterest = interest * 12;
is actually the yearlyInterest which comes to ~ 7 %.

Well, if you are calculating the total with the 'monthly' interest, the formula should have been

C++ Syntax (Toggle Plain Text)
  1. total = principle * pow(1 + interest , term);

For a constant monthly payment, divide the total by 360(the number of months).
With the default values, I got the total to be ~1.57 million ,with a monthly payment of 4376.65.

But if you are calculating annually, the total would turn to be a little less.

And you should be calculating the total after the user input
C++ Syntax (Toggle Plain Text)
  1. //User input
  2. .
  3. .
  4. total = ...
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: can someone tell me why it it can't read from ifstream but works with console,
Next Thread in C++ Forum Timeline: functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC