954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using Math.pow()

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!

webmasts
Newbie Poster
21 posts since Feb 2006
Reputation Points: 10
Solved Threads: 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]

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
// 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]

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?

webmasts
Newbie Poster
21 posts since Feb 2006
Reputation Points: 10
Solved Threads: 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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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!!

psodhi
Newbie Poster
11 posts since Mar 2006
Reputation Points: 10
Solved Threads: 1
 

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.)

gulaygulay2000
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

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.

extemer
Junior Poster
Banned
188 posts since Apr 2010
Reputation Points: -7
Solved Threads: 10
 

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);
	}
}
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

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

Donkerster
Newbie Poster
1 post since Jun 2011
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You