# Thread: Calculating Squares and Cubes in a table

1. Member
Join Date
Mar 2010
Posts
5
Rep Power
0

## Calculating Squares and Cubes in a table

Hey All,

I'm sorry if I sound rushed, but I need some help with one of my assignments. I am having some issues coding my "for" loops. I have an assignment that requests a table that receives an integer input. Once I input an integer, the table will display all of the squared and cubed numbers of every number leading up to that input. See example below:

enter integer: 3

Number Square Cube
1 1 1
2 4 8
3 9 27

continue? (y/n):

The code I have just keeps repeating and does not increment. I tried incrementing up to the input, but that didn't work either. Any help is greatly appreciated. Thanks.

-Aldorfski

See code below:

Java Code:
```import java.util.Scanner;

public class PowerTableDisplayer {

public static void main(String[] args) {
// TODO Auto-generated method stub

// Code to display "Welcome to the Squares and Cubes Table
System.out.println("Welcome to the Squares and Cubes Table");
System.out.println();

// Create a Scanner object
Scanner sc = new Scanner(System.in);

String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get the input from the user
System.out.println("Enter an Integer: ");
int integerNext = sc.nextInt();

System.out.println("Number" + "  " + "Squared" + "  " + "Cubed");
System.out.println("======" + "  " + "======" + "  " + "======");

for (int i = 1; i > 0; i++)
{
i = integerNext;
int numberSquared = (int) Math.pow(i, 2);
int numberCubed = (int) Math.pow (i, 3);

String message = "\n" + i + "       " + numberSquared + "       " + numberCubed;

System.out.println(message);
System.out.println();

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();

}

}

}

}```
Last edited by Eranga; 03-23-2010 at 02:37 AM. Reason: added code tags

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
4
Well, when you start your for loop, you set the value of i to integerNext, so every iteration works with the same value. I think a much better way would be like this:
Java Code:
```for(int i = 1; i <= integerNext; i++) {
//the rest of the code goes here
}```
And learn to use code tags and proper indentation, readability is important, especially if you want others to read your code.

3. Member
Join Date
Mar 2010
Posts
5
Rep Power
0

## Getting there

Hey MoonChile,

Thanks for your reply. After implementing the change, the program ends after the first line.

Enter integer: 3

Number Squared Cubed
3 9 27

Any thoughts on how I can get the Number 1 and 2 to show up, as well as the Square and Cubed numbers for each?

-Aldorfski

4. Remove this line from the body of your loop:

Java Code:
`i = integerNext;`
kind regards,

Jos

5. Member
Join Date
Mar 2010
Posts
5
Rep Power
0

## Thanks

Hey All,

Thanks for your help. This is great.

-Aldorfski

#### Posting Permissions

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