Reputation: 9
The program should output the following using a nested for loop and preferably not use the pow()
method if possible. Any help or advice is appreciated.Expected result:
1 1 1 1 1 1
2 4 8 16 32 64
3 9 27 81 243 729
4 16 64 256 1024 4096
My attempt at it:
class TableOfPowers
{
public static void main(String [] args)
{
int startValue = 1;
int y = 1;
for (int row =0; row < 4; row++)
{
for (int col = startValue; col < startValue+6; col ++)
{
y = y *startValue;
System.out.print(y + " " );
}
System.out.println();
startValue++;
}
}
}
Upvotes: 0
Views: 1075
Reputation: 9
Got it to work, here is my answer:
class TableOfPowers
{
public static void main(String [] args)
{
int startValue = 1;
for (int row =0; row < 4; row++)
{
int y =1;
for (int col = startValue; col < startValue+6; col ++)
{
y = y *startValue;
System.out.print(y + " " );
}
System.out.println();
startValue++;
}
}
}
Upvotes: 1