PowerTable.java.html

  1  /**
  2     This program prints a table of powers of x.
  3  */
  4  public class PowerTable
  5  {
  6     public static void main(String[] args)
  7     {  
  8        final int NMAX = 4;
  9        final double XMAX = 10;
 10  
 11        // Print table header
 12        
 13        for (int n = 1; n <= NMAX; n++)
 14        {
 15           System.out.printf("%10d", n);
 16        }
 17        System.out.println();
 18        for (int n = 1; n <= NMAX; n++)
 19        {
 20           System.out.printf("%10s", "x ");
 21        }
 22        System.out.println();
 23  
 24        // Print table body
 25  
 26        for (double x = 1; x <= XMAX; x++)
 27        {
 28           // Print table row
 29           
 30           for (int n = 1; n <= NMAX; n++)
 31           {
 32              System.out.printf("%10.0f", Math.pow(x, n));
 33           }
 34           System.out.println();
 35        }
 36     }
 37  }