Results 1 to 11 of 11

# Thread: Powes of 7

- 03-03-2011, 03:20 AM #1Member
- 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.

- 03-03-2011, 03:24 AM #2

- 03-03-2011, 04:15 AM #3Member
- Join Date
- Mar 2011
- Posts
- 7

- Rep Power
- 0

Thanks for the reply. The code is as below-

answer 1-

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;

}

}

}

Answer 2-

- 03-03-2011, 04:40 AM #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

- 03-03-2011, 03:33 PM #5Member
- Join Date
- Mar 2011
- Posts
- 7

- Rep Power
- 0

Thanks your help again.

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 #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3

- Rep Power
- 10

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 02:59 PM. Reason: Edited to correct min and max int bit representation

- 03-03-2011, 07:41 PM #7
Cross posted

OTN Discussion Forums : Powers of 7 from 0 to 30 ...

db

- 03-03-2011, 08:22 PM #8Senior Member
- Join Date
- Nov 2010
- Posts
- 210

- Rep Power
- 6

- 03-03-2011, 08:32 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3

- Rep Power
- 10

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

- 03-03-2011, 08:47 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,292
- Blog Entries
- 7

- Rep Power
- 24

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,

JosThe only person who got everything done by Friday was Robinson Crusoe.

- 03-04-2011, 03:42 AM #11Member
- Join Date
- Mar 2011
- Posts
- 7

- Rep Power
- 0

## Bookmarks