January 06, 2015

How to create a Magic square in java

Java code to print Odd Magic Square Matrix

The code is working fine for only 3 x 3 matrix.


/**
 * @author micromax
 *
 */
public class MagicSquare {
   
    private int [][]magicSquare = null;
   
    private void computerMagicSquare(int matrixOrder) {
        // TODO Auto-generated method stub
        int initialValue = (matrixOrder/2);
        int row = initialValue;
        int col = initialValue;
        int loopCount = matrixOrder * matrixOrder;
        int maxMatrixSize = matrixOrder-1;
        magicSquare = new int[matrixOrder][matrixOrder];
       
       
        for(int value=1; value <=loopCount; value++){ // loop value from 1 to square(order)
            //System.out.println(" value :"+value +" Row :"+row+" Col :"+col);
            magicSquare[row][col] = value;
            row = row + 1;
            col = col - 1;
            if(col<0){ col=maxMatrixSize;}
            if(row>maxMatrixSize){ row=0;}
            if(magicSquare[row][col]>0){
                row=row-1;
                col=col+2;
                if(col>maxMatrixSize)
                    col=0;
                if(row<0)
                    row=maxMatrixSize;
            }
        }
}
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MagicSquare magicSquare = new MagicSquare();
        //System.out.println(" Enter the order of matrix ");
       
        //Scanner scanner = new Scanner(System.in);
        //int matrixOrder = scanner.nextInt();
        int matrixOrder = 3;
        magicSquare.computerMagicSquare(matrixOrder);
        magicSquare.dispalyMagicSquare(matrixOrder);

    }

    private void dispalyMagicSquare(int matrixOrder) {
        // TODO Auto-generated method stub
        System.out.println("Displaying Magic Matrix ");
        for(int row=0; row<matrixOrder; row++){
            for(int col=0; col<matrixOrder; col++){
                System.out.print(magicSquare[row][col]+"\t");
            }
            System.out.println("\n");
        }
       
    }



}

  


 Output:





Author +belazy



 http://javabelazy.blogspot.in/

2 comments:

  1. AnonymousJuly 02, 2019

    Hello there, I found your site by way of Google at the same time as
    looking for a related subject, your site came up, it
    looks great. I have bookmarked it in my google bookmarks.

    Hi there, just was aware of your blog through
    Google, and found that it is really informative. I am going to watch
    out for brussels. I will appreciate if you proceed this in future.
    A lot of other people shall be benefited from your writing.
    Cheers!

    ReplyDelete
  2. AnonymousJuly 11, 2019

    Pretty nice post. I just stumbled upon your blog and wished to say that I've
    truly enjoyed browsing your blog posts. In any case I'll be subscribing to your feed and
    I hope you write again very soon!

    ReplyDelete

Your feedback may help others !!!

Facebook comments