int[][] multtable = new int[13][13];
for(int i=0; i<=12;i++){
for(int j=0; j<=12;j++){
//Print the first row (i==0) which is the column header. 4 spaces before the number if number <10 else 3 spaces
if((i==0)&(j>0)){
multtable[0][j] = j;
System.out.print((multtable[i][j] < 10? " ":" ") + multtable[i][j]);
continue;
}
//Print the first column (j==0) which is the numbers (i) itself. 4 spaces before the number if number < 10 else 3 spaces
if((i>0)&& (j==0)){
multtable[i][0] = i;
System.out.print((multtable[i][j] < 10? " ":" ") + multtable[i][j]);
continue;
}
//For other rows (>0) and columns(>0) multiply i & j to create the multiplication table
multtable[i][j] = i*j;
//For values < 10 print 4 spaces before the number
if (multtable[i][j]<10)
System.out.print(" "+multtable[i][j]); else
//For values <100 print 3 spaces before the number
if (multtable[i][j]<100)
System.out.print(" " + multtable[i][j]);else
//Other values (<10) print 2 spaces before the number
System.out.print(" " + multtable[i][j]);
}
//Print line for next row
System.out.println();
}
The output looks like this:
No comments:
Post a Comment