12 Replies Latest reply: Mar 6, 2011 10:32 PM by 841370

# Powers of 7 from 0 to 30

Hi guys,

I want to print this out below-

7 ^ 0 = 1
7 ^ 1 = 7
7 ^ 2 = 49
------------
------------
7 ^ 30 =

I tried below code but it doesn't produce what I want -

public class PowersOfSeven {

public static void main (String [] args) {

int power = 1;

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

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

power = power * 7;

}

}
}

Thanks.
• ###### 1. Re: Powers of 7 from 0 to 30
Take a look at BigInteger.
• ###### 2. Re: Powers of 7 from 0 to 30
edit: nvm. Too late.
• ###### 3. Re: Powers of 7 from 0 to 30
841460 wrote:
I tried below code but it doesn't produce what I want -
And?

What does it produce?
• ###### 4. Re: Powers of 7 from 0 to 30
My guess is pepperoni pizzas! Maybe even with mushrooms!
• ###### 5. Re: Powers of 7 from 0 to 30
Cross posted
http://www.java-forums.org/new-java/39862-powes-7-a.html

db
• ###### 6. Re: Powers of 7 from 0 to 30
Forget it. Jverd beat me to it :-).

Winston
• ###### 7. Re: Powers of 7 from 0 to 30
Darryl Burke wrote:
Cross posted
http://www.java-forums.org/new-java/39862-powes-7-a.html

db
@OP: Don't do that. It's rude. It's very annoying to spend time trying to help someone, only to find out we've been wasting our time repeating what's already been said in another thread.
• ###### 8. Re: Powers of 7 from 0 to 30
It worked for me. What is the problem???
``````          int power = 1;
for (int i = 0; i <= 30; i++){
System.out.println ("7 ^ " + i + " = " + power);
power = power * 7;
}

Output:
7 ^ 0 = 1
7 ^ 1 = 7
7 ^ 2 = 49
7 ^ 3 = 343
7 ^ 4 = 2401
7 ^ 5 = 16807
ect...``````
Sorry, I don't understand what the problem is. Your code works fine.
• ###### 9. Re: Powers of 7 from 0 to 30
It worked for me.
It executed for you. It didn't 'work' at all. It's not the same thing. Did you check the results?
Sorry, I don't understand what the problem is. Your code works fine.
Apart from the integer overflow.
• ###### 10. Re: Powers of 7 from 0 to 30
EJP wrote:
Apart from the integer overflow.
Yep, I didn't notice that. Woops
• ###### 11. Re: Powers of 7 from 0 to 30
Hi,

Use BigInteger:
``````BigInteger power = BigInteger.ONE;
BigInteger seven = new BigInteger("7");
for (int count = 0; count <= 30; count++){
System.out.println ("7 ^ " + count + " = " + power);
power = power.multiply(seven);
}  ``````
Regards

Venkata Sundar M
• ###### 12. Re: Powers of 7 from 0 to 30
The OP was told that four days ago.