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

I need to create a monthly loan Calculator in C++. I know the formula but can't...
I need to create a monthly loan Calculator in C++. I know the formula but can't figure out what's wrong with my code. Any clarification would be appreciated! // Only add code where indicated by the comments. // Do not modify any other code. #include <iostream> #include <cmath> using namespace std; int main() {    // ---------------- Add code here --------------------    // -- Declare necessary variables here              --        int years = 0; //n     int LoanAmount =...
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...
I'm trying to get my occupancy rate to output as 2 decimal places with a %...
I'm trying to get my occupancy rate to output as 2 decimal places with a % sign. I know this is a string but I need it to print as 2 decimal places like 59.23% or 25.36% I have system.out.printf("Occupancy rate: %2s", occupancy + "%"); but I'm still not getting 2 decimal places or the % sign. How can I fix this? I'm in beginning java so I can't use anything advanced. public class ResortAccomidations { public static void main(String[]...
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...
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...
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(); } } });
Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
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...
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--;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT