Not Yet Answered # Using Math.pow()

iamthwee 1,547 Discussion Starter webmasts Phaelax 52 psodhi gulaygulay2000 BestJewSinceJC 700 extemer -17 tong1 22 I am wondering if there would be any chance that someone could help me out with this, I am trying to teach myself c++ which i find it really interesting and I came accross this and would love to see how it works so i could do it and learn ...

1

```
// 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]

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?

0

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

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

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

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.

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

1

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);
}
}
```

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

Recommended Articles

Hi!

I have two tables, I will call them first-half and second-half (of the year)

```
Table: first
id jan feb mar apr
-----------------------
2 80 90 70 60
3 50 40 60 30
Table: second
id jan feb mar apr
-----------------------
2 50 70 60 90
3 80 60 50 ...
```

```
from turtle import *
import random
def allTriMedian (w=300):
speed (0)
vertices = []
point = turtle.Point(x,y)
for i in range (3):
x = random.randint(0,300)
y = random.randint(0,300)
vertices.append(trutle.Point(x,y))
point = turtle.Point(x,y)
triangle = turtle.Polygon(vertices)
a = triangle.side()
b = triangle.side()
c = triangle.side()
m1 = tirangle.median
m2 = triangle.median ...
```