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'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...
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 having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT