Question

In: Computer Science

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 = 0;
    double AnnualRate = 0.0; //r
    double payment = 0.0;
  
    // --
    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;
   years = years * 12;

   payment = ((LoanAmount * AnnualRate) / 1 - (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 << payment << endl;

  
    return 0;
}

Solutions

Expert Solution

Code:

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

int main()
{
   int years;
   int loanAmount;
   double annualRate;
   double payment;

   // getting input
   cout << "Enter the amount, rate as a percentage (e.g. 3.25), and number of years" << endl;
   cout << "separated by spaces: " << endl;
   cin >> loanAmount >> annualRate >> years;

   annualRate = annualRate/100; // converting rate into decimal
   years = years*12; // number of months

   // payment using formula
   payment = (loanAmount*annualRate)/(1 - pow(1+annualRate, -years));

   // output
   cout << payment << endl;

   return 0;
}

OUTPUT:


Related Solutions

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....
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...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
**I need to know how to do it by had without a financial calculator or excel....
**I need to know how to do it by had without a financial calculator or excel. Unsure of what formula to use.** If a 4-year bond with a 7% coupon and a 10% yield to maturity is currently worth $904.90, how much will it be worth 1 year from now if interest rates are constant? A)$947.93 B) $925.39 C) $904.90 D)$1,000.00
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
The monthly payment on a loan may be calculated by the following formula: Payment =    Rate *...
The monthly payment on a loan may be calculated by the following formula: Payment =    Rate * (1 + Rate)N ((1 + Rate)N -1) Rate is the monthly interest rate, which is the annual interest rate divided by 12. (12 percent annual interest would be 1 percent monthly interest.) N is the number of payments, and L is the amount of the loan. Write a program that asks for these (Input) values then displays a report similar to: Loan Amount:            $...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method. This application needs to hold the user input's numbers as an array. Thanks for your time. It is a great help!
I need the calculation of this problem. This question has solution but I can't figure the...
I need the calculation of this problem. This question has solution but I can't figure the answer calculation Again I need calculation probably last line Question : On a certain standardized test The mean is 51 The standard deviation is 15 Exactly 66% of the people who took the test scored higher than Mr. Peterson. Find a, b, and c such that Mr. Peterson's score is approximately a + b ⋅ Φc(d) Do not make a continuity correction. What is...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE...
********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** According to the Organization for Economic Co-Operation and Development (OECD), adults in the United States worked an average of 1,805 hours in 2007. Assume the population standard deviation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT