Page 594 - Computer science 868 Class 12
P. 594

26             }
                27             up++;le++;dn--;rt--;

                28           }
                29       }
                30       void display()

                31       { for(int i=0;i<s;i++)

                32         { for(int j=0;j<s;j++)
                33           { System.out.print(a[i][j]+"\t");}
                34           System.out.println();

                35          }
                36       }

                37       public static void main(int x)
                38       { Spiral ob=new Spiral(x);

                39        ob.generate();
                40         ob.display();

                41          }
                42       }

              Output of the preceding program is as follows:

              1      2       3
              8      9       4
              7      6       5



                Program 11     Magic Square
                               Write a program to create a magic square of size n, where n should be of odd dimension.
                               A magic square has its row, column, diagonal sum equal. The details of the class is given
                               below:

                               Data Members
                               int n                        :  to store array size
                               int a[][]                    :  a double-dimensional array to store the magic square
                               Member Functions
                               magic_square()               :  A default constructor to initialise n to 0
                               void magic()                 :   to construct the magic square
                               void display()               :  to display the magic square

                 1       import java.util.*;
                 2       class magic_square

                 3       {




                592592  Touchpad Computer Science-XII
   589   590   591   592   593   594   595   596   597   598   599