Jump to content

Java Table of Power Help

SlaughterApollo

Doing my last minute summer homework for my AP computer science class and I need a little help. I need to output in the console strictly

a    b    pow( a , b )

1    2     1

2    3     8

3    4     81  

 

And so one. This would be a breeze for me in visual basic but im very new with java can someone help me out?

Link to comment
Share on other sites

Link to post
Share on other sites

Doing my last minute summer homework for my AP computer science class and I need a little help. I need to output in the console strictly

a    b    pow(a, B)

1    2     1

2    3     8

3    4     81  

 

And so one. This would be a breeze for me in visual basic but im very new with java can someone help me out?

while (b>1){

a*=a;

b--;

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

a little explanation please?

While b>1

Take a and multiply it by a, and store it to total. 

reduce b by 1 

 

 

Whoops, I actually noticed a mistake. 

 

It should be: 

 

int total = 0;

while (b>1){

total = a*a;

b--;

}

 

P.s. you might want to use double for total instead of int as a double can hold a much larger value than an int--so something like 100000^1000000 would exceed what can be stored in an int. So: 

 

double total = 0.0;while (b>1){total = a*a;b--;}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

While b>1

Take a and multiply it by a, and store it to total. 

reduce b by 1 

 

 

Whoops, I actually noticed a mistake. 

 

It should be: 

 

int total = 0;

while (b>1){

total = a*a;

b--;

}

 

P.s. you might want to use double for total instead of int as a double can hold a much larger value than an int--so something like 100000^1000000 would exceed what can be stored in an int. So: 

double total = 0.0;while (b>1){total = a*a;b--;}

My confusion comes in at the point of making it display like a table

Link to comment
Share on other sites

Link to post
Share on other sites

My confusion comes in at the point of making it display like a table

ah I didn't put it into a table, I just did the actual math. To put it into a table:

while (b>1){       total = a*a       b--;}System.out.print(a + " " + b + " " + total + "\n");

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

while (b>1){       total = a*a       b--;}System.out.print(a + " " + b + " " + total + "\n");

that formula does not calculate any power correctly apart from squares

anyway, if the OP just wanted to know how to print a table, that's it

the algorithm for powers shouldn't be too hard to figure out, or he can just use Math.pow, but it works with doubles so he might want to implement his own function

Link to comment
Share on other sites

Link to post
Share on other sites

that formula does not calculate any power correctly apart from squares

anyway, if the OP just wanted to know how to print a table, that's it

the algorithm for powers shouldn't be too hard to figure out, or he can just use Math.pow, but it works with doubles so he might want to implement his own function

Whoops, 

double a = 2; double b = 0;double power = b;double base = a; if (b==0)       System.out.print(base + " " + power + " " + 1 + "\n");else {    while (b>1){        a *= base;         b--;     }    System.out.print(base + " " + power + " " + a + "\n");}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Whoops, 

double base = a; while (b>1){     a *= base;      b--; }System.out.print(base + " " + b + " " + a + "\n");
Fails for exponent = 0 :)
Link to comment
Share on other sites

Link to post
Share on other sites

Fails for exponent = 0 :)

Way to post before I got a chance to  edit due to my internet dropping out. 

 

/see above. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Way to post before I got a chance to  edit due to my internet dropping out. 

 

/see above.

Yup, that's correct

The 0 case can also not be treated as a special case, like this

int result = 1;while(b--)        result *= a;
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×