Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Math.pow?

 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi techies,
Kindly help me the situation below:



Any help is appreciated
Thanks,
Bonny
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by introducing the "a" variable. It needs to increase in each iteration.
 
Henry Wong
author
Marshal
Pie
Posts: 21399
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not completely sure what you are asking... but why not? Sure, the Math.pow() method can be used here to raise the percentage to the number of years.

Henry
 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am particularly interested in the code that can make solve that problem based on the formula given and the scenario as a whole.

But Ulf, how can I introduce the 'a' variable to make the iteration?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, the loop needs to change. "for(n=1; n<=10; n++);" does nothing; instead, use something like
 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
Thanks alot for replying.
Your code:

What I did:

The output is: The amount at the end of year 11 is:52500.0
This amount is correct but why does it print year 11?
Also, how could I use this amount as the value of P for the second and so on till the 10th year?
I am a complete newbie in the Java world but please help.
Thanks alot
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code doesn't even compile; are you sure it's the code that you're running? If you fix the syntax errors to make it compile, then it will indeed run from 1 to 10 for n. What's still missing is that the formula for "a" isn't what's mentioned in the description (and hence doesn't change for higher "n").
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ UD: Thanks for your contribution, but we don't want to rob Ogwal of an opportunity to learn: JavaRanch is NotACodeMill ]
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

I realised it.
Thanks.
 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf & Priety,
Thanks for the comments. I have taken note.
Below is my refined code:

The ouput iterates ten times, incrementing 'n' but keeping same value of 'a'.
Kindly help me understand how I can increment 'n' first, before 'a' is calculated based on the formula.
Also, is there a way I could use the output when 'n=1' as the value of P when 'n=2' and so on till year 10?

**Priety**..I understand the meaning of JavaRanch is 'NotACodeMill http://faq.javaranch.com/java/NotACodeMill' but I am trying to come up atleast with something as per what I know so far...

Thanks again for the time. It's appreciated
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calculating "a" only once - outside of the loop; you should move that statement into the loop.
 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
Thanks alot. It works.
But one more thing, is there a way I could use the output when 'n=1' as the value of P when 'n=2' and so on till year 10?
Whatever your believe is, remained blessed...
Regards,
Ogwal
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ogwal,

Good that you have achieved what you were trying to do.

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ogwal,

I have a doubt.

As far as I understand your requirement is like this:

Calculate the interest for 1 year on the beginning principal of 50000 using rate of interest as 5%.

Then for the 2nd year add the interest to the principal and recalculate the interest on it at 5% and so on for 10 years.

Is this right?


Then the calculation should show the following results:

The amount at the end of the 1 th year is:52500.0
The amount at the end of the 2 th year is:55125.0
The amount at the end of the 3 th year is:57881.2
The amount at the end of the 4 th year is:60775.3
The amount at the end of the 5 th year is:63814.0

When I ran your code the results were:

The amount at the end of the 1 th year is:52500.0
The amount at the end of the 2 th year is:57881.2
The amount at the end of the 3 th year is:67004.7
The amount at the end of the 4 th year is:81444.7
The amount at the end of the 5 th year is:103946.4

So is that code producing the right results?
Just curions.
Correct me if I am wrong.


 
Campbell Ritchie
Sheriff
Pie
Posts: 49774
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gave the correct results when I tried it. What are you doing differently?
 
Ogwal Boniface
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prietty,
Below is my complete code and the subsequent output:

My main problem is to use like the value of year1 in the output as the value of P for the second year and so on, giving something as below:
year 1 is 52,500.00
year 2 is 57,881.25
year 3 is 67,004.78
year 4 is 81,444.73
year 5 is 103,946.41
year 6 is 139,298.13
year 7 is 196,006.46
year 8 is 289,590.81
year 9 is 449,250.39
year 10 is 731,781.55
Grateful for further assistance
Regards
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It gave the correct results when I tried it. What are you doing differently?


If its giving the correct results. Then its done.
If the formula mentioned in the spec. is just to be implemented in java.
I have no doubts then.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic