# In desperate need of help. Making an exponents table.

• February 14th, 2012, 02:49 AM
jp93129
In desperate need of help. Making an exponents table.
Hello everyone!

So I'm just at a wall right now. I'm a very very beginner java programmer. I currently have to write a program that displays a table. The user will enter the base number, then the maximum exponent. It will then be displayed like so:

Enter the base: 2
Enter the maximum exponent: 7
The base is 2 and the maximum exponent is 7.

Powers of 2

x 2^x
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128

I have to use for loops.

If someone could explain a loop that can carry out this function, then it would be greatly greatly appreciated.

I can post what I currently have in order to see I am making an effort (Although the for loops are way way off.) I am not on here to ask for straight answers, but to instead build onto my knowledge of Java. Any help is appreciated and thank you very much in advance!

Here's my code: (Totally lost)

Code :

``` import java.util.*;   public class Practice {   public static void main(String[] args) {   Scanner kb = new Scanner(System.in); int b = kb.nextInt(); int y = kb.nextInt(); int result = b ^ y;     for(result = 1; result <= y; result = result * b){ System.out.print("\t " + result); System.out.print("\n");     } } }```
• February 14th, 2012, 06:37 AM
Norm
Re: In desperate need of help. Making an exponents table.
Please post what you have so far.
Start with a variable with a value of 1 and multiply it by the base and save that value and then loop and do it again until done.
• February 14th, 2012, 07:38 AM
shiftasterisk
Re: In desperate need of help. Making an exponents table.
There only needs to be 1 for loop. Once you collected base 'b' and exponent 'e', the for loop would run from x = 1 to x<= e. Inside the for loop just print out b and b^x.
• February 14th, 2012, 03:23 PM
jp93129
Re: In desperate need of help. Making an exponents table.
Thanks for the replies, working on it now! Just posted the code I have now, if anyone could elaborate a little more it'd be nice. I think i know what to put in the loop now, but i don't think i understand the concept of a for loop.
• February 14th, 2012, 03:33 PM
Norm
Re: In desperate need of help. Making an exponents table.
BB Code List - Java Programming Forums - The Java Community

You should use descriptive variable names, not x and y. The two values you are readin g in are base and exponent.

To see how a loop works, change your code to loop exponent times and print out the value of the index each time the loop iterates.
• February 14th, 2012, 03:37 PM
jp93129
Re: In desperate need of help. Making an exponents table.
I understand you up until you say "Inside the for loop just print out b and b^x. " Do you mean put that in the update part? I understand for(x=1; x<= exponent; ???)

I also dont understand what should be in the System.out.println(???); after the loop.
• February 14th, 2012, 03:39 PM
Norm
Re: In desperate need of help. Making an exponents table.
For testing just print out the value of the index.

When you can get the loop working like you want, then add code to compute the value to be printed.

Quote:

what should be in the System.out.println(???)
The variable that contains the computed amount.
• February 14th, 2012, 03:50 PM
jp93129
Re: In desperate need of help. Making an exponents table.
I'm so frustrated! lol. Well I'm really having trouble with the update part of the loop now. I can't get it right.
• February 14th, 2012, 03:54 PM
Norm
Re: In desperate need of help. Making an exponents table.
Do things one at a time:
make a loop that goes for exponent times
when that works
compute the value inside of the loop using the base and loop index
print the value
• February 14th, 2012, 04:01 PM
jp93129
Re: In desperate need of help. Making an exponents table.
Oh i see. So there is some expression(s) I'm missing before the loop I think. I'm going to experiment some more, if I don't figure it out ill be more specific and takes things one at a time. I really appreciate your help!
• February 14th, 2012, 07:59 PM
shiftasterisk
Re: In desperate need of help. Making an exponents table.
First lets change your variables to something more descriptive

Code :

``` Scanner kb = new Scanner(System.in); int base = kb.nextInt(); int exponent = kb.nextInt();```

Alright so before leaving the for loop in terms of x was a bad idea because it wasn't informative enough.

Here's the basic for loop

Code :

`for (int exp = 1; exp <= exponent; x++)`

As you can see exp will start at 1, increments on each iteration and ends at the 'exponent' which was inputted by the user. This will be the first column (x) as described in your first post. Now that you have your first column (x), you just need your second column (base^x).

So inside the loop will print 'base' (the first column) and then print 'base^exp' (your second column)

Do you understand it now?
• February 14th, 2012, 08:02 PM
Norm
Re: In desperate need of help. Making an exponents table.
@shiftasterisk
Please run your code through a compiler before posting it here. Your for statement is going to confuse the OP.
• February 14th, 2012, 08:56 PM
jp93129
Re: In desperate need of help. Making an exponents table.
Quote:

Originally Posted by shiftasterisk
First lets change your variables to something more descriptive

Code :

``` Scanner kb = new Scanner(System.in); int base = kb.nextInt(); int exponent = kb.nextInt();```

Alright so before leaving the for loop in terms of x was a bad idea because it wasn't informative enough.

Here's the basic for loop

Code :

`for (int exp = 1; int x <= exponent; x++)`

As you can see exp will start at 1, increments on each iteration and ends at the 'exponent' which was inputted by the user. This will be the first column (x) as described in your first post. Now that you have your first column (x), you just need your second column (base^x).

So inside the loop will print 'base' (the first column) and then print 'base^exp' (your second column)

Do you understand it now?

Figured it out. Notice the important piece of code that was left out after the for loop.

Code :

```import java.util.*;   public class Project1 {   public static void main(String[] args) {   Scanner kb = new Scanner(System.in); int b = kb.nextInt(); int y = kb.nextInt(); int result = 1;   for(int i = 0; i<=y; i++) {   System.out.println(i + "\t" +result);     result *= b; // pre-calculate for next test }     } }```
• February 15th, 2012, 09:49 AM
shiftasterisk
Re: In desperate need of help. Making an exponents table.
Quote:

Originally Posted by Norm
@shiftasterisk
Please run your code through a compiler before posting it here. Your for statement is going to confuse the OP.

yea whoops, really botched that one as i was in a rush, sorry about that. Glad the OP figured it out, I edited my post just in case for future references tho.