1. Member
Join Date
Mar 2011
Posts
7
Rep Power
0

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

2. Originally Posted by himanwish
What is the best thing to do?
Post your code so we can see what you have done.

3. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
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;

}

}
}

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

5. Member
Join Date
Mar 2011
Posts
7
Rep Power
0

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.

6. 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)
Last edited by sunde887; 03-04-2011 at 01:59 PM. Reason: Edited to correct min and max int bit representation

7. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
3
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.

8. Thanks for catching that, was typing on my phone and rushing

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

10. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Thanks very much for those who helped me out.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•