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);
Related
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();
}
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;
}
So in my intro to java book, I was tasked with this:
Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In one year, the tuition will be $10,500. Write a program that computes the tuition in ten years and the total cost of four years' worth of tuition after the tenth year.
I can calculate the tenth year tuition easily enough, but what has me stumped is how to add the unique tuition values at years 11, 12, 13 and 14. If I'm right, it should add up to 73717.764259. What my code is giving me is 158394.52795515177. As code goes, It may just be that i'm thinking about this in the wrong way and that my code did the addition correctly, but I think I'm more right here. Here's the code i'm using now:
double initialTuition = 10000;
final double theRate = 0.05;
for (int i = 1; i < 15; i++) {
initialTuition = ((theRate * initialTuition) + initialTuition);
System.out.println("Year " + i + " tuition is: " + initialTuition);
while (i == 10){
System.out.println("Year " + i + " tuition is: " + initialTuition);
double startOfFourYearTuition = initialTuition;
System.out.println(startOfFourYearTuition);
break;
}
while ((i > 10) && (i < 15)) {
System.out.println("Year " + i + " tuition is: " + initialTuition);
initialTuition += initialTuition;
break;
}
}
The last while loop is my attempt at adding the 4 years.
To reiterate the question, How could I pull out the unique values of initialTuition at iterations 11 to 14 and add them?
You also have a lot of gratuitous code in there, while loops inside of for loops, etc. Try this on for size:
public static void main(String[] args)
{
double initialTuition = 10000;
double summarizedTuition = 0;
final double theRate = 0.05;
for (int i = 1; i < 15; i++) {
initialTuition = ((theRate * initialTuition) + initialTuition);
System.out.println("Year " + i + " tuition is: " + initialTuition);
if ((i > 10) && (i < 15))
{
summarizedTuition += initialTuition;
}
}
System.out.println("Summarized tuition for years 11 - 14: " + summarizedTuition);
}
The output is:
Year 1 tuition is: 10500.0
Year 2 tuition is: 11025.0
Year 3 tuition is: 11576.25
Year 4 tuition is: 12155.0625
Year 5 tuition is: 12762.815625
Year 6 tuition is: 13400.95640625
Year 7 tuition is: 14071.0042265625
Year 8 tuition is: 14774.554437890625
Year 9 tuition is: 15513.282159785156
Year 10 tuition is: 16288.946267774414
Year 11 tuition is: 17103.393581163135
Year 12 tuition is: 17958.56326022129
Year 13 tuition is: 18856.491423232354
Year 14 tuition is: 19799.31599439397
Summarized tuition for years 11 - 14: 73717.76425901074
double theRate = 1.05; // rate of increase +5%
double tuitionCost = 10000; // base cost
double fourYearCost = 0; // four year cost (years 10-13)
for (int i = 0; i <= 13; i++) {
System.out.println("Cost of year: " + i + " is $" + tuitionCost); // print out this years cost
if(i >= 10){ //if its year 10, 11, 12, 13
fourYearCost += tuitionCost; // add this years tuition cost
}
tuitionCost *= theRate; // increment the tuition (mutiply it by 1.05, or 5% increase)
}
System.out.println("Cost of tuition for years 10, 11, 12, 13: $" + fourYearCost); // print the cost for the remaining years
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).");
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++;
}