``````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?
``````

## Recommended Answers

Can you post the output from the code?

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

That will depend on the initial value of Principal and the rest of the code.

## All 5 Replies

Can you post the output from the code?

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

That will depend on the initial value of Principal and the rest of the code.

ok it was my fault. Sorry for posting this. :(

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.