I am showing the declining balance of a loan per year and the interest paid per year. The first and second year are always messed up, causing the rest of the years to not be correct. Also, how do I print a column with designated space for each entry, besides using spaces to separate them like I did?
My code:
int year;
periods = loanDurationYears*12;
double annualBalance = 0;
double annualInterest = 0;
int month = loanDurationYears-(loanDurationYears-1);
balance = loanAmount;
double interestForMonth = balance*((interestRate*.01)/12);
double principalForMonth = monthlyPayment - interestForMonth;
balance = balance - principalForMonth;
System.out.println("Annual balances");
System.out.printf("%s %s %s \n","Year","Interest","Balance");
for(int j=0;j<periods;j++)
{
month++;
year = month/12;
interestForMonth = balance*((interestRate*.01)/12);
principalForMonth = monthlyPayment - interestForMonth;
balance = balance - principalForMonth;
annualBalance = annualBalance + balance;
annualInterest = annualInterest + interestForMonth;
if(month%12 == 0)
{
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
annualBalance = 0;
annualInterest = 0;
}
}
My output:
Year Interest Balance
1 4852.43 859718.74
2 5080.12 899718.26
3 4842.34 857208.50
4 4588.01 811738.89
5 4315.96 763103.31
6 4024.98 711081.35
7 3713.73 655437.20
8 3380.81 595918.66
9 3024.71 532255.97
10 2643.82 464160.58
11 2236.40 391323.84
12 1800.62 313415.64
13 1334.49 230082.85
14 835.91 140947.76
15 302.62 45606.39
How the output should look: (besides the columns not being aligned)
Year Interest Loan Balance
1 5965.23 86408.21
2 5715.14 82566.33
3 5447.64 78456.94
4 5161.51 74061.43
5 4855.46 69359.87
6 4528.10 64330.94
7 4177.95 58951.87
8 3803.41 53198.26
9 3402.80 47044.03
10 2974.29 40461.31
11 2515.95 33420.24
12 2025.70 25888.91
13 1501.31 17833.19
14 940.40 9216.58
15 340.45 -0.00
You are getting an incorrect starting amount because you adjust the balance before the for-loop. It looks like your for-loop expects to start with the full balance, but it's already been reduced. You should also either move the increment of month to the end of the loop, or start it at zero instead of 1.
This line will always initialize month to 1. Why bother with the math?
int month = loanDurationYears-(loanDurationYears-1);
I haven't tested this or anything but it should work. I'm sure it's not the best fix but something simple like this should work fine.
It simply checks the size of the variable and then uses different print statements depending on the size of it. Also as the year throws off alignment I've put some code that should fix that
if(month%12 == 0)
{
// Set the amount of decimals for all interest rates
DecimalFormat df = new DecimalFormat("#.##");
df.format(annualInterest);
// Get the length of annualInterest
int n = math.floor(annualInterest);
int length = (int)(Math.log10(n)+1);
if (length = 3)
{
// Have 1 less space than normal on the print statement
// Maybe also do a check on the year also as that throws it out when it goes past 10
if (year > 10)
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
else
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
}
if (length = 2)
{
// Have 2 less spaces than normal
}
annualBalance = 0;
annualInterest = 0;
}
Related
I need to make graph-like matrix when a user enters their paychecks for all 12 months of year. Simplegraph will print output, rounding entered paycheck to nearest 1000.
To be specific, my output needs to look like the picture below. For example: if a user enters a value between 1501 and 2500, it would be rounded to 2000.
I have figured out how to round the values, but I can't figure out how to place an X for the correct value in the appointed month.
I suppose that I need to save the values in an array. However, if I save them in an array, I can't figure out how to place them in their appropriate month.
one entry per month in the array?
when looping and creating the matrix just have a method like
for y
for x
if(shouldPutX(x,y,months))
System.out.print("X")
else
System.out.print(" ")
shouldPutX(x,y,months){
int monthVal = month[x-1]
if monthVal should be rounded to this line
return true
else
return false
}
// creating scanner with 12 spaces (12 months)
int[] salaryIntArray = new int[12];
Scanner salary = new Scanner(System.in);
// counters
int i = 0;
int j = 0;
// entering salary for every month in array
while (i<salaryIntArray.length){
System.out.println("Enter syour salary:");
salaryIntArray [i] = salary.nextInt();
i++;
}
// trying to round numbers, but it doesnt work with arrays, only when I'am using simple integer variables
// Rules to round numbers:
// 0 - 500 round to 0
// 501 - 1500 round to 1000
// 1501 - 2500 round to 2000
// 2501 - 3500 round to 3000
// 3501 - 4500 round to 4000
// 4501 - 5500 round to 5000
int roundNumberArray = ((salaryIntArray + 499) / 1000 * 1000);
// System.out.println(roundedNumber);
// System.out.println();
// Test print to check to see if salaries are correctly saved
System.out.println();
int month = 1;
for (int j = 0; j<12; j++){
System.out.println("Salary for " + month + " month is " + salaryIntArray[i1]);
month ++;
}
What I need now is to how to make table/matrix
I'm taking a course in Java and we haven't officially learned if statements yet. I was studying and saw this question:
Write a method called pay that accepts two parameters: a real number for a TA's salary, and an integer for the number hours the TA worked this week. The method should return how much money to pay the TA. For example, the call pay(5.50, 6) should return 33.0. The TA should receive "overtime" pay of 1.5 times the normal salary for any hours above 8. For example, the call pay(4.00, 11) should return (4.00 * 8) + (6.00 * 3) or 50.0.
How do you solve this without using if statements? So far I've got this but I'm stuck on regular pay:
public static double pay (double salary, int hours) {
double pay = 0;
for (int i = hours; i > 8; i --) {
pay += (salary * 1.5);
}
}
To avoid direct use of flow control statements like if or while you can use Math.min and Math.max. For this particular problem using a loop would not be efficient either.
They may technically use an if statements or the equivalent, but so do a lot of your other standard library calls you already make:
public static double pay (double salary, int hours) {
int hoursWorkedRegularTime = Math.min(8, hours);
int hoursWorkedOverTime = Math.max(0, hours - 8);
return (hoursWorkedRegularTime * salary) +
(hoursWorkedOverTime * (salary * 1.5));
}
Since you've used a for loop, here's a solution just using two for loops.
public static double pay (double salary, int hours) {
double pay = 0;
for (int i = 0; i < hours && i < 8; i++) {
pay += salary;
}
for (int i = 8; i < hours; i++) {
pay += (salary * 1.5);
}
return pay;
}
This sums the salary for the regular hours up to 8, and then sums the salary for the overtime hours, where the overtime hours are paid at 1.5 * salary.
If there are no overtime hours, the second for loop will not be entered and will have no effect.
There's a few ways you can go about this, but it's hard to know what's allowed (if you can't even use if).
I would recommend using a while loop:
double pay = 0;
while (hoursWorked > 8) {
pay += (salary * 1.5);
hoursWorked--;
}
pay += (hoursWorked * salary);
The reason why this works is it decrements your hoursWorked to a value that is guaranteed to be less than or equal to 8 (assuming hoursWorked and salary are both greater than 0). If hoursWorked <= 8, then it will never enter the while loop.
If you really want to get hacky, you could use bitwise operators:
int otherHours = hours - 8;
int multiplier = (~otherHours & 0x80000000) >>> 31;
otherHours *= multiplier;
return otherHours * 0.5 * salary + hours * salary;
So basically, if otherHours is negative, there should be no overpay. We do this by selecting the sign bit of otherHours and shifting it to the least significant bit (with 0 padding) to mean either 1 or 0. After first negating it (if sign bit is 1, multiplier should be 0).
When you multiply this with otherHours it will be 0 in the case there are less than 8 hours, so as not to accidentally subtract any pay, when doing the final calculation.
Just for the record, here is a solution quite close to where you were stopped :
public static double pay (double salary, int hours) {
double pay = salary * hours;
for (int i = hours; i > 8; i --) {
pay += salary * 0.5;
}
}
You can simply use a ternary operator ?::
pay = hours*salary + ((hours > 8) ? (hours-8)*salary*0.5 : 0);
— pay a standard salary for the whole time worked, plus 50% for time above 8 hours (if any).
A cast to int can be abused for this purpose.
Note that the function
f(x) = 10/9 - 1/(x+1) = 1 + 1/9 - 1/(x+1)
is between 0 and 1 (exclusive) for 0 <= x < 8 and between 1 and 1.2 for x >= 8. Casting this value to int results 0 for x < 8 and in 1 for x >= 8.
This can be used in the calculation of the result:
public static double pay(double salary, int hours) {
int overtime = (int)(10d/9d - 1d/(hours+1));
return salary * (hours + 0.5 * overtime * (hours - 8));
}
Here's a way to do it using truncation from integer division, something that you probably have learnt at the start of java courses. Essentially the solution is a one liner that does not need if, loops, comparisons, libraries.
public static double pay(double salary, int hours) {
//Pay all hours as overtime, and then subtract the extra from the first 8 hours
double p1 = (hours * 1.5 * salary) - (4 * salary);
//In the case where the TA works for less than 8 hours,
//subtract all the extra so that ultimately, pay = salary * hours
double p2 = (hours * 0.5 * salary) - (4 * salary);
//When theres no overtime worked, m equals to 1.
//When there are overtime hours, m is equals to 0.
int m = (8 + 7) / (hours + 7);
//When there are overtime hours, pay is simply equal to p1.
//When there are no overtime hours, p2 is subtracted from p1.
return p1 - m*p2;
}
A solution which does not use any conditional(implicit or explicit)
Practically, you need to calculate hours * rate but if you have overtime then you need to add a bonus of the form overtime_hours * overtime_rate
in pseudo-code:
//you need to return:
hours * rate + is_overtime * overtime_time * overtime_rate
where
is_overtime = ceiling ( x / (x+1)) # this will be zero when x == 0, in rest 1
x = integer_division(hours, 8) # x == 0 if no overtime, in rest a positive integer
overtime_time = hours - 8
overtime_rate = (1.5 - 1) * rate = 0.5 * rate
(((hours/8)-1)*8 + hours%8)*salary*0.5 + (hours*salary)
overtime*salary*0.5 + (hours*salary)
((( 11/8 -1)*8 + 11%8)* 4*0.5 + ( 11* 4) = 50
(( 1 -1)*8 + 3)* 2 + 44 = 50
(( 0)*8 + 3)* 2 + 44 = 50
(( 0 + 3)* 2 + 44 = 50
6 + 44 = 50
So suppose we have (17 hours, 4 salary)
(((17/8)-1)*8 + 17%8)*4*0.5 + 17*4 = 86
( (2 -1)*8 + 1)*4*0.5 + 68 = 86
(8 + 1)*2 + 68 = 86
9*2 + 68 = 86
18 + 68 = 86
17-8=9 is overtime
9*4*1.5 + 8*4 = 9*6 + 32 = 54 + 32 = 86
You could creatively use a while statement as an if statement
while(hours > 8){
return ((hours - 8) * salary * 1.5) + (8 * salary);
}
return hours * salary;
Plenty of good and more efficient answers already, but here's another simple option using a while loop and the ternary operator:
double pay = 0.0;
while(hours > 0){
pay += hours > 8 ? wage * 1.5 : wage;
hours--;
}
return pay;
Using tanh to decide whether the hours are below 8 or not:
public static double pay (double salary, int hours) {
int decider = (int)(tanh(hours - 8) / 2 + 1);
double overtimeCompensation = 1.5;
double result = salary * (hours * (1 - decider) + (8 + (hours - 8) * overtimeCompensation) * decider);
return result;
}
The decider variable is 0 when hours is less than 8, otherwise 1. The equation basically contains two parts: the first hours * (1 - decider) would be for hours < 8, and (8 + (hours - 8) * overtimeCompensation) * decider for when hours >= 8. If hours < 8, then 1 - decider is 1 and decider is 0, so using the equations first part. And if hours >= 8, 1 - decider is 0 and decider is 1, so the opposite happens, the first part of the equation is 0 and the second is multiplied by 1.
public static double pay (double salary, int hours) {
int extra_hours = hours - 8;
extra_hours = extra_hours > 0 ? extra_hours : 0;
double extra_salary = (salary * 1.5) * extra_hours;
double normal_salary = extra_hours > 0 ? salary * 8 : salary * hours;
return normal_salary + extra_salary;
}
You can use ternary operator.
public static double pay(double salary, int hours){
//get the salary and hours
return hours>8?(1.5*hours-4)*salary:hours*salary;
}
Some programming languages have explicit pattern-matching features. So for example, in XSLT, a given template will fire if the node being processed matches a given XPATH query better than other templates in your programme.
This kind of "declarative" programming is a level of abstraction higher than what you have in Java, but you still have the switch statement, which gives you the ability to control your flow with a "matching" approach rather than using explicit if-else constructs.
public static double pay (double salary, int hours) {
double pay = 0;
for (int i = hours; i > 0;i--){
switch (i){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
pay += (salary);
break;
default:
pay += (salary * 1.5);
break;
}
}
return pay;
}
Having said that, for your specific example, what you really do need is an if statement. The switch approach will work, but it's a bit contrived.
In Tsql
DECLARE #amount float = 4.00
, #hrs int = 11
DECLARE #overtime int
, #overeight bit
SET #overeight = ((#hrs/8) ^ 1)
SET #overtime = CEILING((#hrs%8) / ((#hrs%8) + 1.0)) * ~#overeight
--SELECT #hrs * #amount
SELECT ((#hrs-(#hrs%8 * ~#overeight)) * #amount) + ( #overtime * (#hrs%8 * (#amount * 1.5)))
(from Valentin above)
I am currently trying to develop a compound interest calculator that includes monthly contributions. I have successfully been able to get the compound interest calculation working without the monthly contributions using the following line of code, but cannot figure out what the formula should be when adding monthly contributions.
double calculatedValue = (principalValue * Math.pow(1 + (interestRateValue/numberOfCompoundsValue), (termValue * numberOfCompoundsValue)));
When trying to get the calculated value with contributions I changed the way this is done. See the following code how I approached this.
//The starting principal
double principalValue = 5000;
//Interest rate (%)
double interestRateValue = 0.05;
//How many times a year to add interest
int numberOfCompoundsValue = 4;
//The number of years used for the calculation
double termValue = 30;
//The monthly contribution amount
double monthlyContributionsValue = 400;
//How often interest is added. E.g. Every 3 months if adding interest 4 times in a year
int interestAddedEveryXMonths = 12/numberOfCompoundsValue;
//The total number of months for the calculation
int totalNumberOfMonths = (int)(12 * termValue);
for(int i = 1; i <= totalNumberOfMonths; i++)
{
principalValue += monthlyContributionsValue;
if(i % interestAddedEveryXMonths == 0)
{
principalValue += (principalValue * interestRateValue);
}
}
I figured this should do what I am after. Every month increase the principal by the contribution amount and if that month equals a month where interest should be added then calculate the interest * the interest rate and add that to the principal.
When using the values above I expect the answer $355,242.18 but get $10511941.97, which looks better in my bank account but not in my calculation.
If anyone can offer me some help or point out where I have gone wrong that would be much appreciated.
Thanks in advance
Your problem is here:
principalValue += (principalValue * interestRateValue);
You're adding a full year's interest every quarter, when you should be adding just a quarter's interest. You need to scale that interest rate down to get the right rate.
Here's an example:
class CashFlow {
private final double initialDeposit;
private final double rate;
private final int years;
private final double monthlyContribution;
private final int interestFrequency;
CashFlow(double initialDeposit, double rate, int years,
double monthlyContribution, int interestFrequency) {
if ( years < 1 ) {
throw new IllegalArgumentException("years must be at least 1");
}
if ( rate <= 0 ) {
throw new IllegalArgumentException("rate must be positive");
}
if ( 12 % interestFrequency != 0 ) {
throw new IllegalArgumentException("frequency must divide 12");
}
this.initialDeposit = initialDeposit;
this.rate = rate;
this.years = years;
this.monthlyContribution = monthlyContribution;
this.interestFrequency = interestFrequency;
}
public double terminalValue() {
final int interestPeriod = 12 / interestFrequency;
final double pRate = Math.pow(1 + rate, 1.0 / interestPeriod) - 1;
double value = initialDeposit;
for ( int i = 0; i < years * 12; ++i ) {
value += monthlyContribution;
if ( i % interestFrequency == interestFrequency - 1 ) {
value *= 1 + pRate;
}
}
return value;
}
}
class CompoundCalc {
public static void main(String[] args) {
CashFlow cf = new CashFlow(5000, 0.05, 30, 400, 3);
System.out.println("Terminal value: " + cf.terminalValue());
}
}
with output:
run:
Terminal value: 350421.2302849443
BUILD SUCCESSFUL (total time: 0 seconds)
which is close to the $355k value you found.
There are a number of different conventions you could use to get the quarterly rate. Dividing the annual rate by 4 is a simple and practical one, but the pow(1 + rate, 1 / 4) - 1 method above is more theoretically sound, since it's mathematically equivalent to the corresponding annual rate.
After some brief testing I've come to the conclusion that either you have:
miscalculated the value you want ($355,242.18)
OR
incorrectly asked your question
The calculation you've described that you want ($5000 start + $400 monthly contributions for 30 years + interest every 3 months) is found by the code you've provided. The value that it gives ($10,511,941.97) is indeed correct from what I can see. The only other suggestions I can offer are to only use double if you need to (for example termValue can be an int) AND when ever you know the value is not going to change (for example interestRateValue) use final. It will help avoid any unforeseen error in larger programs. I hope this helps you figure out your interest calculator or answers any questions you have.
static void Main(string[] args)
{
double monthlyDeposit;
double rateOfInterest;
double numberOfCompounds;
double years;
double futureValue = 0;
double totalAmount = 0;
Console.WriteLine("Compound Interest Calculation based on monthly deposits");
Console.WriteLine("Monthly Deposit");
monthlyDeposit = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Rate Of Interest");
rateOfInterest = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Number of Compounds in a year");
numberOfCompounds = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Number of year");
years = Convert.ToDouble(Console.ReadLine());
futureValue = monthlyDeposit;
for (int i = 1; i <= years * 12; i++)
{
totalAmount = futureValue * (1 + (rateOfInterest / 100) / 12);
if (i == years * 12)
futureValue = totalAmount;
else
futureValue = totalAmount + monthlyDeposit;
}
Console.WriteLine("Future Value is=" + futureValue);
Console.ReadLine();
}
//Output
Compound Interest Calculation based on monthly Deposits
Monthly Deposit
1500
Rate Of Interest
7.5
Number of Compounds in a year
12
Number of year
1
Future Value is=18748.2726237313
In my code I am having the user type in 3 things, and for the third input I ask for the number of years. However in my for loop I can't use the variable that I ask the user to input. For example if the user were to input "3", my code would do 15 years (which is the max).
double yearInvest;
double interRate;
double numOfYears = 3;
double amountBeforeTotal;
double amountTotal;
double years;
System.out.println("Compound Interest \n");
System.out.println("This program will print out a title table that will "
+ " display the amount of a yearly investment over a period of "
+ " up to 15 years. \n");
Scanner input = new Scanner(System.in);
System.out.println("Enter the yearly investment:");
yearInvest = input.nextDouble();
System.out.println("Enter the interest rate (%):");
interRate = input.nextDouble();
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; 15 >= years; years++) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account",
"Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
}
P.S. I am still working on the code and it is not fully done. And I would also like some advice if possible.
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; 15 >= years; years++)
Your code is getting a user inputted numOfyears which for example would be 3, for your case. Your loop is from (1..15) no matter what, since your loop's 2nd parameter is: 15 >= years.
What you are looking for is (1..numOfYears)
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; years <= numOfYears; years++)
//...more code
There are a few things that I notice about your code that might not be exactly what you want.
Firstly you are storing all of your variables as doubles which are mainly used for storing floating point (i.e. numbers with a decimal place). In place of double it might be better to use int.
Next your for loop is always going to loop 15 times with years 1 to 15. If you want this to be only numOfYears times you should have a loop that compares to numOfYears
for (years = 1; years <= numOfYears; years++) {
//TODO
}
Lastly something that is quite important for coding and something that it is easy to ignore if teaching yourself is style.
for (years = 1; 15>=years; years++ ) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account", "Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
}
This indentation gives a much clearer view of what is in your for loop and helps debugging and readablilty
You can try this if you want to print only 15 years interest rates.
for (years = 1; numOfYears >= years; years++) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account",
"Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
if(years == 15) {
break;
}
}
This will print the interest of 15 years if user enters any number greater than 15 or else will print interest rates of all years if user enters number < 15.
Ok so this java program I'm working on is supposed to compute an investment value over the course of 30 years. It asks the user where their starting investment is and the percentage rate (in the form of decimals). I thought I had it all figured out but my program is returning some ridiculous values. Can someone take a look at my code and tell me what I did wrong?
These are the sample outputs provided to me
What is the amount invested? 1000
What is the annual interest rate? .09
Years Future Value
----- ------------
1 $1093.81
2 $1196.41
...
29 $13467.25
30 $14730.58
my output is returning values in billions and trillions of dollars...just crazy stuff. The formula I have been provided with is
futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12
here is the code for my program
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.DecimalFormat;
public class InvestmentValue
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
double investmentAmmnt;
// monthly interest rate
double mri;
int years;
System.out.print("What is the ammount invested? ");
investmentAmmnt = input.nextDouble();
System.out.print("What is the annual interest rate? ");
mri = input.nextDouble();
futureInvestmentValue(investmentAmmnt, mri, 30);
}
public static double futureInvestmentValue(double investmentAmmnt, double mri, int years)
{
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
System.out.println("Annual interest rate: " + mri);
System.out.println("Years \t \t Future Value");
for (int i = 1; i <= years * 12; i++){
investmentAmmnt = investmentAmmnt * Math.pow(1 + (mri / 12),(years * 12));
if (i % 12 == 0){
System.out.println(i / 12 + "\t\t" + (df.format(investmentAmmnt)));
}
}
return investmentAmmnt;
}
}
The problem is that the formula futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12 calculates the value of the investment for any amount of years in the future. The problem is that you for loop is calculating more than it needs to. That formula only needs to be done once. Your function futureInvestmentValue should not have a for loop.
Here's how it works:
public static double futureInvestmentValue(final double investmentAmmnt, double mri, int years)
{
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
double amount = investmentAmmnt;
System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
System.out.println("Annual interest rate: " + mri);
System.out.println("Years \t \t Future Value");
for (int i = 1; i <= years ; i++){
amount = investmentAmmnt * Math.pow(1 + (mri /100),(i ));
System.out.println(i + "\t\t" + (df.format(amount)));
}
return amount;
}
The problems are many with your code...
Your Increasing the amount with Math.pow() in a loop - that's why it goes wild.
Your loop iterates over years * 12 (months?)
Your doing wild *12 and /12 calculations you don't need.
--> output
Years Future Value
1 $1,030.00
2 $1,060.90
3 $1,092.73
4 $1,125.51
5 $1,159.27
6 $1,194.05
7 $1,229.87
8 $1,266.77
9 $1,304.77
10 $1,343.92
11 $1,384.23
12 $1,425.76
13 $1,468.53
14 $1,512.59
15 $1,557.97
16 $1,604.71
17 $1,652.85
18 $1,702.43
19 $1,753.51
20 $1,806.11
21 $1,860.29
22 $1,916.10
23 $1,973.59
24 $2,032.79
25 $2,093.78
26 $2,156.59
27 $2,221.29
28 $2,287.93
29 $2,356.57
30 $2,427.26