/** * Creates a multiplication table. * * @author TK Rogers * @version 12-23-10 */ public class MultiplicationTable1 { public static void main ( ) { // Good programming practice: use a variable constant // (final) if a number is used in multiple places, then // only one line of code must be altered to change the size. final int N = 10 ;
// for-loop below centers title. Why use a for-loop? // if the size of the table is changed the title stays // approximately in the center for ( int v = 2 ; v <= N / 2 ; v++ ) { System.out.print ( "\t" ) ; // Tabs N/2 times. }
// table's title System.out.print ( "Multiplication Table \n\n") ;
// for-loop below prints x values. for ( int z = 2 ; z <= N ; z++ ) { System.out.print ( "\t" + z ) ; } System.out.println ( "\n" ) ;
// for-loop below prints y values & x * y products. for ( int x = 2 ; x <= N ; x++ ) { System.out.print ( x ) ; for ( int y = 2 ; y <= N ; y++ ) { System.out.print ( "\t" + ( x * y ) ) ; } System.out.println ( "\n" ) ; } } }