Question:

Magic Square!! Can Everyone give me the java code of this.. Please I need your help

by  |  earlier

0 LIKES UnLike



Magic Square

Arrange the number 1-9 in the boxes below such that each row and each column adds up to 15. Use each number only once.

*******

*6*1*8* 15

*******

*7*5*3* 15

*******

*2*9*4* 15

*******

15 15 15

Congratulations! You solved the Magic Square!

Write a complete Java program that will ask the user to input an integer number from 1 to 9 only and specify the row and column where the input will be stored. After inputting a number display the magic square with the corresponding sum for each row and column. Check the sum if it equals to 15.

Sample run:

C:\>java MagicSquare<press enter>

*******

*0*0*0* 0

*******

*0*0*0* 0

*******

*0*0*0* 0

*******

0 0 0

Row: 0<press enter>

Column: 0<press enter>

Value: 6<press enter>

*******

*6*0*0* 6

*******

*0*0*0* 0

*******

*0*0*0* 0

*******

6 0 0

Row: 1<press enter>

Column:1<press enter>

Value: 5<press enter>

*******

*6*0*0* 6

*******

*0*5*0* 5

*******

*0*0*0* 0

*******

6 5 0

Row: 2<press enter>

Column:2<press enter>

Value: 4<press enter>

*******

*6*0*0* 6

*******

*0*5*0* 5

*******

*0*0*4* 4

*******

6 5 4

Row: 0<press enter>

Column:1<press enter>

Value: 1<press enter>

*******

*6*1*0* 7

*******

*0*5*0* 5

*******

*0*0*4* 4

*******

6 6 4

Row: 0<press enter>

Column:2<press enter>

Value: 8<press enter>

*******

*6*1*8* 15

*******

*0*5*0* 5

*******

*0*0*4* 4

*******

6 6 12

And so forth and so on until the sum of row and column is 15 the program will be automatically terminated. The program can be terminated if the input for the row, column and value are -1.

*******

*0*0*0* 0

*******

*0*0*0* 0

*******

*0*0*0* 0

*******

0 0 0

Row: -1<press enter>

Column: -1<press enter>

Value: -1<press enter>

Program Terminated!

------------Thanks-----------

try to answer.. thanks again

 Tags:

   Report

1 ANSWERS


  1. import java.util.Scanner;

    public class MagicSquares

    {

        static int magic_square[][] = new int[3][3];

        static int sum[] = new int[6];

        static Scanner scanner = new Scanner(System.in);

        public static void main(String[] args)

        {

            int value = 0;

            int i, j = 0;

            while (true)

            {

                System.out.println();

                System.out.print(&quot;Row: &quot;);

                i = scanner.nextInt();

                System.out.print(&quot;Column: &quot;);

                j = scanner.nextInt();

                System.out.print(&quot;Value: &quot;);

                value = scanner.nextInt();

                if ((value &lt; 0) &amp;&amp; (i &lt; 0) &amp;&amp; (j &lt; 0))

                {

                    System.out.println(&quot;Program Terminated!&quot;);

                    break;

                }

                magic_square[i][j] = value;

                if (count())

                {

                    System.out

                            .println(&quot;Congratulations! You solved the Magic Square!&quot;);

                    print_magic_square();

                    break;

                }

                print_magic_square();

            }

            System.out.println(&quot;Game over&quot;);

        }

        static boolean count()

        {

            boolean all15 = true;

            sum[0] = magic_square[0][0] + magic_square[0][1] + magic_square[0][2];

            sum[1] = magic_square[1][0] + magic_square[1][1] + magic_square[1][2];

            sum[2] = magic_square[2][0] + magic_square[2][1] + magic_square[2][2];

            sum[3] = magic_square[0][0] + magic_square[1][0] + magic_square[2][0];

            sum[4] = magic_square[0][1] + magic_square[1][1] + magic_square[2][1];

            sum[5] = magic_square[0][2] + magic_square[1][2] + magic_square[2][2];

            for (int i = 0; i &lt; 6; i++)

            {

                if (sum[i] != 15)

                    all15 = false;

            }

            return all15;

        }

        static void print_magic_square()

        {

            for (int i = 0; i &lt; 3; i++)

            {

                System.out.println(&quot;*******&quot;);

                System.out.print(&quot;*&quot;);

                for (int j = 0; j &lt; 3; j++)

                {

                    System.out.print(magic_square[i][j] + &quot;*&quot;);

                }

                System.out.println(&quot; &quot; + sum[i]);

            }

            System.out.println(&quot;*******&quot;);

            System.out.println(sum[3] + &quot; &quot; + sum[4] + &quot; &quot; + sum[5]);

        }

    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.