1,105,578 Community Members

Using Math.pow()

Member Avatar
webmasts
Newbie Poster
21 posts since Feb 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

HI,

I need some help on using the math.pow() function to solve the following problem:

a^n = p(1+ r)^n

how would I translate it using math.pow()

Thanks for your suggestions and any help!

Member Avatar
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 
// pow(x, y) returns the x raised 
    // to the yth power.
    System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0,2.0));
    System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5));
    System.out.println("pow(8, -1) is " + Math.pow(8,-1));

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

Member Avatar
webmasts
Newbie Poster
21 posts since Feb 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So to check If I understand, let say 6 raised to the power of 4 6^4 would be coded like this:

math.pow(6, 4) do I have to use it in this form...6.0 and 4.0 using double? Will Int work?

Member Avatar
Phaelax
Practically a Posting Shark
880 posts since Mar 2004
Reputation Points: 52 [?]
Q&As Helped to Solve: 54 [?]
Skill Endorsements: 4 [?]
 
0
 

pow takes doubles. Is it really so hard to search for the "Math" class in the API docs?

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

Member Avatar
psodhi
Newbie Poster
11 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

So to check If I understand, let say 6 raised to the power of 4 6^4 would be coded like this:

math.pow(6, 4) do I have to use it in this form...6.0 and 4.0 using double? Will Int work?

Yes..
taking int values will work but then you have to cast the Math.pow() method to return int valus..because it returns double value by default.

e.g., int a=(int)Math.pow(6,4);

Hope it would help u understand the method!!

Member Avatar
gulaygulay2000
Newbie Poster
1 post since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

can you help me create this program?
Create a Java program called FindThatNumber. java. The program should generate 10 random integers between 1 and 100 (inclusive) using Math.pow( ) and then store them sequentially in an array. The program should then ask the user to enter a single integer. If the integer is in the array just entered, the program should respond “Bingo!”; otherwise, the program should respond “Sorry”. The program should continue running until the user enters -999 to quit. (Hint: The code (int) (100 * Math.random( ) + 1) creates the required random number.)

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

4 years ago they might have helped you create it. Now - doubtful. But I'm sure nobody else is going to help you. And btw, you don't use Math.pow() to generate random numbers.

extemer
Junior Poster
188 posts since Apr 2010
Reputation Points: -17 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
Banned
 
-1
 

i recently done a program very much same to your ask question
it works on foramula a=p(1+r)^n.check it out and look that if it can help you and if any question you can ask.

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextArea.*;

   class TextAreaDemoB
{
     public static void main(String[] args){

     double amount,
           p=1000.0,
           rate=.05;

     DecimalFormat pre= new  DecimalFormat("0.00");
     JTextArea out= new JTextArea(0,0);

     out.append(" year\tamount\n");

     for(int year=1; year<=10; year++)
{
     amount=p * Math.pow( 1.0 + rate , year );
     out.append( year + "\t" +
 	 pre.format( amount )+ "\n" );

}

    JOptionPane.showMessageDialog(null,out," note",JOptionPane.INFORMATION_MESSAGE);

}}

remember for taking power of any value you can use Math.pow class.

Member Avatar
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 22 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 0 [?]
 
0
 

A further development of extemer's program leads to the following table showing the furture values of one thousand dolloars investment calculated based on compound interest: a(t) = (1 + i)^t where i is rate while t the number of years

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextArea.*;

   class TextAreaDemoB {
     public static void main(String[] args){

     double amount,
           p=1000.0,
           rate=.025;

     DecimalFormat pre= new  DecimalFormat("0.00");
     DecimalFormat preRate= new DecimalFormat("0.000");
     JTextArea out= new JTextArea(0,0);

     out.append(" year, rate->\t");
     for (int j=0;j<10;j++)
     out.append(preRate.format(rate+j*0.025)+ "\t");
     out.append("\n");

     for(int year=1; year<=10; year++) {
     	out.append( year + "\t");
     	for (int j=0; j<10; j++){
     amount=p * Math.pow( 1.0 + (rate+j*0.025) , year );
 	 out.append(pre.format( amount )+ "\t" );
 	 }
 	 out.append("\n");
 	 }

    JOptionPane.showMessageDialog(null,out,"Future values of $1000 investment with various numbers of years and rates",JOptionPane.INFORMATION_MESSAGE);
	}
}
Member Avatar
Donkerster
Newbie Poster
1 post since Jun 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

(int) Math.pow(a,n) = (int) Math.pow((p(1+ r)),n)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article