import java.io.*; //-------------------------------------------------------------- // MathTable.java // CS100 P3Q1 // // Wei Tsang Ooi 16 July 1999 // // This program prompt the user for an integer n, and prints // out a table of integer 1 .. n, their squares, square roots // and cubes. // //-------------------------------------------------------------- class MathTable { //-------------------------------------------------------------- // getInput // // input : none // output : an integer input by the user // read an integer from user and returns it. //-------------------------------------------------------------- static int getInput() throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter a number : "); return Integer.parseInt(stdin.readLine()); } //-------------------------------------------------------------- // printTableRow // // input : an integer n // print out n, n^2, sqrt(n) and n^3 //-------------------------------------------------------------- static void printTableRow(int n) { int square = n*n; int cube = square*n; double root = Math.sqrt(n); String output = n + "\t" + square + "\t" + cube + "\t" + root; System.out.println(output); } //-------------------------------------------------------------- // printTable // // input : none // print out a table of n, n^2, sqrt(n) and n^3 for 1 .. n //-------------------------------------------------------------- static void printTable(int n) { for (int i = 1; i <= n; i++) { printTableRow(i); } } public static void main (String args[]) throws IOException { int n = getInput(); printTable(n); } }