for (int year = 1; year <= 10; year++){
    amount = principal * Math.pow(1+rate, year);
    principal = amount;
    System.out.printf("%d%20.2f\n",year,amount);


        for (int year = 1; year <= 10; year++){
            amount = principal * Math.pow(1+rate, year);
            System.out.printf("%d%20.2f\n",year,amount);


            // what is the difference between these two? 

Can you post the output from the code?

One problem I see is there is no ending }s for the two {s

As impression I get that you are doing loop to calculate compound interest, but also same time use pow funcition, which does it without loop. Therefore I would say the difference is that first one is even more mixed up than second. But I could be totally wrong.

> for (int year = 1; year <= 10; year++){
>     amount = principal * Math.pow(1+rate, year);
>     principal = amount;
>     System.out.printf("%d%20.2f\n`Inline Code Example Here`",year,amount);

here evey time the loops executes , the vaue of amount gets calculated by the given formula , BUT in next line the principle becomes the amount and next time when amount is calculated its new "principle" in the formula takes the value of previously calculated amount..
i.e value of principle for everytime the loop executes is changes and takes the previous vaue of amount calculated ,, i.e principle is variable with varying vaues evrytime the loop gets executed

if the value of principle initially is 100
then first time loop executed , amount is calculated using principle = 100 , and assume we get the value of amount x
then sexonf time loop executed . amount is calculated using principle = x (what we get the value of amount)

Now

> for (int year = 1; year <= 10; year++){
>             amount = principal * Math.pow(1+rate, year);
>             System.out.printf("%d%20.2f\n",year,amount);

Here the vaue of principle in formula doesn't changes and don't take a new value everytime the loops is exucuted.
here everytime the loop is executed , the principle value is same everytime .

principe is say 100
in second time loop execute then also principle remains 100
in third time loop execute then also principle remains 100

This article has been dead for over six months. Start a new discussion instead.