Question

In: Computer Science

I'm trying to make this C++ loan calculator but I can't get the program to output...

I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision.

This is what I have.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
   // ---------------- Add code here --------------------
   // -- Declare necessary variables here              --
  
    int years = 0;
    int LoanAmount = 0;
    double AnnualRate = 0.0;
    double moPayment = 0.00;
  
    // --
    cout << "Enter the amount, rate as a percentage (e.g. 3.25), and number of years\n";
    cout << " separated by spaces: " << endl;

   // ---------------- Add code here --------------------
   // -- Receive input and compute the monthly payment --
   cin >> LoanAmount >> AnnualRate >> years;

   AnnualRate = AnnualRate / 100;    //rate to decimal
   years = years * 12;   //years to months

   moPayment = (LoanAmount * AnnualRate) / (1 - pow(1 + AnnualRate, -years));
  
   // ---------------- Add code here ------------------
    // Print out the answer as a double, all by itself
    // (no text) followed by a newline
    // Ex. cout << payment << endl;

   cout << moPayment << endl;

  
    return 0;
}

Solutions

Expert Solution

#include <iostream>

#include <cmath>

using namespace std;

int main() {

       // ---------------- Add code here --------------------

       // -- Declare necessary variables here              --

       int years = 0;

       int LoanAmount = 0;

       double AnnualRate = 0.0;

       double moPayment = 0.00;

       // --

       cout << "Enter the amount, rate as a percentage (e.g. 3.25), and number of years\n";

       cout << " separated by spaces: " << endl;

       // ---------------- Add code here --------------------

       // -- Receive input and compute the monthly payment --

       cin >> LoanAmount >> AnnualRate >> years;

       double MonthlyRate = AnnualRate / 1200;    //get the monthly rate to decimal

       years = years * 12;   //years to months

       // for monthly payment, monthly rate is required not Annual Rate

       moPayment = (LoanAmount * MonthlyRate) / (1 - pow(1 + MonthlyRate, -years));

       moPayment = round(moPayment*100)/100; // round the result to 2 decimal places

       // ---------------- Add code here ------------------

       // Print out the answer as a double, all by itself

       // (no text) followed by a newline

       // Ex. cout << payment << endl;

       cout << moPayment << endl;

       return 0;

}

//end of program

Output:


Related Solutions

C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
Hi! I kep trying this question and can't get it right. At a certain temperature, the...
Hi! I kep trying this question and can't get it right. At a certain temperature, the equilibrium constant for the following chemical equation is 2.90. At this temperature, calculate the number of moles of NO2(g) that must be added to 2.73 mol of SO2(g) in order to form 1.30 mol of SO3(g) at equilibrium. SO2(g) + NO2(g) ----> SO3 (g) + NO (g) Answer in mol NO2
I am struggling with this assignment. I can't get the program to run when I enter...
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated. Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable...
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
C++ program please, can you show me where I did wrong. I'm trying to print the...
C++ program please, can you show me where I did wrong. I'm trying to print the counter-clockwise spiral form using int *p, but my output turned out weird, thank you void makeSpiral(int *p, int rows, int cols) { int left = 0, value = 1, top = 0; while(left < cols && top < rows) { for(int i = top;i < rows;++i) { *(p+i*cols+left) = value++; } left++; for(int i = left;i < cols;++i) { *(p+(rows-1)*cols+i) = value++; } rows--;...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to...
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to populate the username in the program after entered. I can enter a name and the three scores and average them as the program needs but the name is not adding next to the "Students name: " in the program. Any help would be appreciated and please place a note for what I am doing wrong. Thank you using System; using System.Collections.Generic; using System.Linq; using...
(C++) Hey, so I'm trying to make a Remove() and Add() function for a Binary Search...
(C++) Hey, so I'm trying to make a Remove() and Add() function for a Binary Search Tree (NOT USING RECURSION), can you help? For 3 cases: no child, 1 child and 2 children also for Remove(). This is the function to find the node to be deleted/added TreeNode* PrivateFind(const T& tWhat)    {        //Start from head        TreeNode* tWalk = mHead;        while (tWalk != nullptr)        {            //If found then return...
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT