Java Loan Calculator nested for loop based on user input - java

The task is to create a loan calculator based on the user input of min and max years of loan payments, loan amount, and min and max % rate with incremented value given by user as well for rate and number of years.
Desired output should look like this:
Principle: $275000.0
Years to repay: 10
Interest Monthly
Rate Payment
6.25 3087.7
6.75 3157.66
7.25 3228.53
Principle: $275000.0
Years to repay: 15
Interest Monthly
Rate Payment
6.25 2357.91
6.75 2433.5
7.25 2510.37
Principle: $275000.0
Years to repay: 20
Interest Monthly
Rate Payment
6.25 2010.05
6.75 2091.0
7.25 2173.53
Please help me fix errors. Thanks!
public static void main(String[] args){
Scanner console = new Scanner (System.in);
System.out.println("This program computes monthly " + "mortgage payments.");
System.out.print("Enter the loan amount: ");
double loan = console.nextDouble();
System.out.print("Enter the starting number of years to repay the loan: ");
int startingYears = console.nextInt();
System.out.print("Enter the ending number of years to repay the loan: ");
int endingYears = console.nextInt();
System.out.print("Enter the years increment between tables: ");
int incrementYears = console.nextInt();
System.out.print("Enter the starting loan yearly interest rate, %: ");
double startingRate = console.nextDouble();
System.out.print("Enter the ending loan yearly interest rate, %: ");
double endingRate = console.nextDouble();
System.out.print("Enter the increment interest rate, %: ");
double incrementRate = console.nextDouble();
System.out.println();
System.out.println("Principle: $" + (double) loan);
System.out.printf("Years to repay: %d\n", startingYears);
System.out.println("-------- -------");
double payment;
System.out.println("Rate\tPayment");
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++){
int n = 12 * startingYears;
double c = startingRate / 12.0 / 100.0;
payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);
System.out.println(i + " " + payment);
// System.out.println(round2(startingRate) + "\t" + round2(payment));
startingYears += incrementYears;
}
}
}
}

for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++) {
You are never incrementing the values of your counters i.e. i and j and hence the values of i and j will always be equal to the initialized values and these loops will run for infinite times.
Increment both the counters to be able to reach the termination condition i.e. j <= endingYears and i <= endingRate respectively.
Here is the code snippet:
System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
System.out.printf("Years to repay: %d\n", j);
System.out.println("-------- -------");
System.out.println("Rate\tPayment");
for (double i = startingRate; i <= endingRate; i+=incrementRate){
int n = 12 * j;
double c = i / 12.0 / 100.0;
double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1);
System.out.println(i + "\t" + payment);
}
System.out.println();
}

Related

Return to start of loop java

This is a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8.
I may have over-indulged myself here as I'm pretty new to programming, but I'm in too deep and want to figure this out.
With my current code the first line outputs correctly, displaying the rate, total and monthly. However, after that the code just continues to output the inner most loop. I need to return to the start of the loop. It would greatly be appreciated if you could point me in the right direction.
P.S. I am aware that my padding isn't in place. My biggest concern is getting the algorithm down on "paper" first, and then worrying about the beauty of it.
package loancalc194;
import java.util.Scanner;
public class LoanCalc194 {
public static void main(String[] args) {
//LOAN CALCULATOR
//user enters loan AMOUNT, loan PERIOD(years),
//output displays MONTHLY and TOTAL payments
//^displayed per 1/8 increment from 5-8% interest
Scanner in = new Scanner(System.in);
//prompt user
//retrieve loan amount and period
System.out.print("Please enter the loan amount: ");
double amount = in.nextDouble();
System.out.print("Please enter the loan period (years): ");
int period = in.nextInt();
//INTEREST LOOP///////////////////////////////////////////////////
double interest = .05000;
double inc = 0.00125;
interest = interest + inc;
double monthly = 0;
double total = 0;
System.out.println("Interest Rate\t\t\tTotal Payment\tMonthly Payment");
while (interest < .08){
//interest = interest + inc;
System.out.print(interest + "\t");
while (true){
total = (amount * period) + (interest * amount);
System.out.print("\t" + total + "\t\t");
while (true) {
monthly = ((total / period) / 12);
System.out.println(monthly);
//interest = interest + inc;
}
}
}
One loop should be enough for what you want.
Your two while(true) loops do nothing else than looping on the same values forever.
In the following code, the interest gets incremented and the new calculations computed at each loop until interest reaches your max value, this is probably what you need.
double interest = .05000;
double inc = 0.00125;
double monthly = 0;
double total = 0;
while (interest < .08){
System.out.print(interest + "\t");
total = (amount * period) + (interest * amount);
System.out.print("\t" + total + "\t\t");
monthly = ((total / period) / 12);
System.out.println(monthly);
interest = interest + inc;
}

How to create a savings bank system where program stops looping after balance doubles

I am a tasked to create a program that runs as described, programmed in Java:
Illustrate the growth of money in a savings account. The user enters the initial amount (as a decimal) and the interest rate which are used to calculate the number of years until the money doubles and the number of years until the money reaches a million dollars. (you will need to use more than 1 loop).
Note: The balance at the end of each year is
(1 + r) * balance
where balance is the previous balance, and r is the annual rate of interest in decimal form.
However, I cannot find the error in my code. What happens is that it continuously writes "Your balance will double in " and then the number infinitely grows.
Here is the code:
Scanner input = new Scanner(System.in);
double years = 0;
double futureVaule;
System.out.print("Please enter the amount of money you would like to deposit, as a decimal:\n>>");
double balance = input.nextDouble();
System.out.print("Please enter the interest rate of your deposit, as a percent:\n>>");
double r = input.nextDouble();
do
{
futureValue = (1 + r) * balance;
years = years + 1;
System.out.println("Your balance will double in "
+ Math.round(years) + " year(s).");
}while(futureValue <= balance*2);
This should work
futureValue = balance;
double doubleBalance = balance*2;
while(futureValue <= doubleBalance)
{
futureValue += futureValue * (( 1 + r)/100);
years++;
}
System.out.println("Your balance will double in "
+ years + " year(s).");

Stuck with do-while loop

I am writing a Java program. It supposed to be print out yearend account balance, invested this year, annual return, and number of the year. EX:
Year 1 – Invest $10,000 # 10% you end up with $11,000
Year 2 – Invest another $10,000 on top of the $11,000 you already have so now you have $21,000 and you make $2,100 in annual return so you end up with $23,100
and keep going until reach 6 years.
My code printed all 6 years but with same value as year 1. So anything wrong with the loop? Thanks so much. Here my code:
import java.io.*;
import java.util.*;
import java.text.*;
public class ExamFive {
public static void main(String[] args) {
final int MAX_INVESTMENT = 5000000;
double goalAmount;
double amountInvested;
double annualRate;
double interest;
double total= 0;
int year = 0;
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter your investment goal amount: ");
goalAmount = myScanner.nextDouble();
if (goalAmount > MAX_INVESTMENT) {
System.out.println("Your goal is outside the allowed 5,000,000 limit");
}
System.out.println("Enter the amount annually invested: ");
amountInvested = myScanner.nextDouble();
System.out.println("Enter the expected annual return rate (ex 6.50): ");
annualRate = myScanner.nextDouble();
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
} while (total < MAX_INVESTMENT && year < 6);
System.out.println(" ");
System.out.println("Your investment goal " + goalAmount);
System.out.println("Your annualinvestment amount " + amountInvested);
System.out.println("Number of years to reach your goal " + year);
}
}
Here is the output:
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 1
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 2
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 3
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 4
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 5
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 6
It seems that you're also calculating the interest on the annual savings only.
Change:
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
...
} while (total < MAX_INVESTMENT && year < 6);
To:
do {
total += amountInvested;
interest = total * annualRate / 100;
total += interest;
year++;
...
} while (total < MAX_INVESTMENT && year < 6);
You aren't keeping a running total.
Looks like maybe a 3rd or 4th week tutorial but it's a fair question at least you asked what's wrong.
Try: total += (amountInvested + interest);
parenthesis brackets aren't needed but I usually find them a great logical grouping device.
Check the loop below you are not adding 10,000 as you stated
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
}
Add following line
amountInvested += (total + 10000);
as the last line in do while loop
amountInvested, annualRate are always the same. you are not incrementing or decrementing these values in the loop. So, they will always remain the same. Also, the formula to caculate the interest is incorrect, you are not using year variable anywhere. it should be,
do {
interest = amountInvested * (annualRate / 100) * year;
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
} while (total < MAX_INVESTMENT && year < 6);

calculating interest on a certificate of deposit

I'm working on a program that will calculate the basic interest accrued on a certificate of deposit. The program asks for the amount of money invested and the term (up to five years). Depending on how many years their term is, is what determines how much interest is earned. I use an if/else statement to determine the rate of interest. I then use a loop to print out how much money is in the account at the end of each year. My problem is that when I run the program, the money is not counting.
Here is the entire code.
import java.util.Scanner;
public class CDCalc
{
public static void main(String args[])
{
int Count = 0;
double Rate = 0;
double Total = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("How much money do you want to invest?");
int Invest = userInput.nextInt();
System.out.println("How many years will your term be?");
int Term = userInput.nextInt();
System.out.println("Investing: " + Invest);
System.out.println(" Term: " + Term);
if (Term <= 1)
{
Rate = .3;
}
else if (Term <= 2)
{
Rate = .45;
}
else if (Term <= 3)
{
Rate = .95;
}
else if (Term <= 4)
{
Rate = 1.5;
}
else if (Term <= 5)
{
Rate = 1.8;
}
int count = 1;
while(count <= 5)
{
Total = Invest + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}
}
}
and here is the result I get with a 10 dollar investment, just to keep it simple, and a 5 year investment.
How much money do you want to invest?
10
How many years will your term be?
5
Investing: 10
Term: 5
Value after year 1: 10.18
Value after year 2: 10.18
Value after year 3: 10.18
Value after year 4: 10.18
Value after year 5: 10.18
My main problem is I dont know how to make it continually add the intrest onto the total. I'm not sure if I need to use a different loop or what. Any help would be appreciated.
Total = Invest + (Invest * (Rate) / (100.0));
You are not changing the value of Invest for each year, so it is not compounding. It is like you are getting .18$ of interest each year, retired from the account.
Change Total for Invest.
You need to add the investment interest to your total:
Total = Invest;
int count = 1;
while(count <= 5)
{
Total = Total + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}

Math.pow difficulty

When I enter 1000 for investment amount 4.25 for monthly interest rate and 1 for years, why do I get the result 4.384414858452464E11 instead of the expected 1043.34?
import java.util.Scanner;
public class FinancialApplicationFutureInvestment_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
int investmentAmount = input.nextInt();
System.out.println("Enter monthly interest rate: ");
double monthlyInterestRate = input.nextDouble();
System.out.println("Enter number of years");
int numOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
(Math.pow(1 + monthlyInterestRate, numOfYears * 12));
System.out.println("Accumulated value is: " + futureInvestmentValue);
double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
System.out.println(test);
}
}
Monthly interest rate will probably need to be entered as 0.0425
1 + monthlyInterestRate
Is monthlyInterestRate a raw factor, or is it expressed in percentage points?
Try dividing by one hundred.
the formula is
A = P(1+r/100)^n
so it should be
investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12));
well, you compute 1+4.25 (5.25) as monthly interest rate, instead of 1+(4.25/100) .
you should use casting in System.out.println (double to int) --
(int)(futureInvestmentValue * 100) / 100.0

Categories