Print table program

The problem

Write a program that displays the following table:

aa^2a^3
111
248
3927
41664

Breaking it down

Breaking down the problem we need to print a number, square a number and the number to the 3rd power. Using java 8 IntStream we can create a range of numbers. Starting at 1 and ending at 4, we can format the result in the table using MessageFormat.

public static void main(String[] args) {

    System.out.printf("%3s  |%5s  |%5s\n", "a", "a^2", "a^3");

    IntStream.rangeClosed(1, 4).forEach(
            i -> System.out
                    .printf("%3d  |%5d  |%5d\n", i, i * i, i * i * i));
}

Output

  a  |  a^2  |  a^3
  1  |    1  |    1
  2  |    4  |    8
  3  |    9  |   27
  4  |   16  |   64