Java Data Type How to - Create a table of squares and cubes








Question

We would like to know how to create a table of squares and cubes.

Answer

import java.util.Formatter;
//from   w ww.  j a v  a2s.com
public class Main  {
  public static void main(String args[]) {
    Formatter fmt;

    for (int i = 1; i <= 10; i++) {
      fmt = new Formatter();

      fmt.format("%4d %4d %4d", i, i * i, i * i * i);
      System.out.println(fmt);
    }

  }
}

The code above generates the following result.