# Powes of 7

• 03-03-2011, 03:20 AM
himanwish
Powes of 7
Hi guys,

I need to wirte a program which displays the powers of 7 from 0 to 35.

I tried using a for loop and also Math.pow(7,i) ( i is numbers from 0 to 35, inclusive 0 and 35). But cannot get the proper output ( I can get the output from 0 to 20, but after that it doesn't look happy).

What is the best thing to do?

Thanks.
• 03-03-2011, 03:24 AM
Junky
Quote:

Originally Posted by himanwish
What is the best thing to do?

Post your code so we can see what you have done.
• 03-03-2011, 04:15 AM
himanwish
Thanks for the reply. The code is as below-

public class PowersOfSeven {

public static void main (String [] args) {

int power = 1;

for (int count = 0; count <= 35; count++){

System.out.println ("7 ^ " + count + " = " + power);

power = power * 7;

}

}
}

• 03-03-2011, 04:40 AM
Junky
Your problem is that after a while the value of power will be greater than Integer.MAX_VALUE and therefore wraps around to a negative value.

You may want to check with your teacher. I thought the assignment was asking for multiples of seven. ie
7
14
21
28
35
• 03-03-2011, 03:33 PM
himanwish

It's definitely about powers of 7. The program should print out like below-

7 ^ 0 = 1
7 ^ 1 = 7
7 ^ 2 = 49
..............
.............
.............
.............
7 ^ 35 =

up to 7 ^ 20 I think program runs ok , but after that it doesn't loook right.
• 03-03-2011, 03:48 PM
sunde887
Look into large number types such as long, double and BigInteger. Like junky said, the value is reaching the max int level and overflowing into the negative range.

Integers are only 32 bits and they are signed so the max is 2^31 and -2^31, which is approximately 2.1 bill( I could be off a bit, not around a computer to test it)
• 03-03-2011, 07:41 PM
DarrylBurke
• 03-03-2011, 08:22 PM
Iron Lion
Quote:

Originally Posted by sunde887
Look into large number types such as long, double and BigInteger. Like junky said, the value is reaching the max int level and overflowing into the negative range.

Integers are only 32 bits and they are signed so the max is 2^15 and -2^15, which is approximately 2.1 bill( I could be off a bit, not around a computer to test it)

2^15 is 32768; the sign doesn't take up 17 bits. The max and min int values are (2^31)-1 and -(2^31) respectively.
• 03-03-2011, 08:32 PM
sunde887
Thanks for catching that, was typing on my phone and rushing
• 03-03-2011, 08:47 PM
JosAH
Quote:

Originally Posted by himanwish

It's definitely about powers of 7. The program should print out like below-

7 ^ 0 = 1
7 ^ 1 = 7
7 ^ 2 = 49
..............
.............
.............
.............
7 ^ 35 =

up to 7 ^ 20 I think program runs ok , but after that it doesn't loook right.

Time to switch on your rusty old calculator: press 7 log 10 log / * 10; the answer tells you how many digits are needed to store 7 raised to the power 35. That number is way too large to store in an int. Use BigDecimal number type objects for the calculations.

kind regards,

Jos
• 03-04-2011, 03:42 AM
himanwish
Thanks very much for those who helped me out.