Pattern to be drawn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
class NumberPatt{ public static void main(String args[]){ // declare variables to store column and rows sum int col=0, row=0; int width=9; int mat[][]=new int[width*2][width*2]; for(int i=1;i<=width;i++){ //loop for space for(int j=(width-i);j>0;j--){ System.out.print(" "); } // loop for numbers for(int k=i;k>0;k--){ row+=k; mat[(i-1)][(width-k)] = k; System.out.print(k); } // loop for next numbers for(int l=2;l<=i;l++){ row+=l; mat[(i-1)][((width+l)-1)] = l; System.out.print(l); } // print the sum of current column System.out.println("\t\tCol Sum is : " + row); row=0; } // we got the pattern in matrix. // Now print rows' sum for(int i=0;i<(width);i++){ int sum=0; for(int j=0;j<(width);j++){ sum+=mat[j][i]; } System.out.println("Col " + i + " sum " + sum); } } } |
Output