Create the following program using java or anything to compa

Create the following program using java, or anything to compare what i have been doing. Thanks

Write a program that accepts 9 values from the user in the range of 1 to 9. Store these values in a two-dimensional array of size 3*3. The program should display if the two-dimensional array is a magic square or not. A square is considered to be a magic square if all of its row, column and diagonal sums are equal. Create a method called magicSquare () that accepts the two-dimensional array with integers as input and returns true or false. For example: [[2, 7, 6], [9, 5, 1], [4, 3, 8]] is a magic square because all eight of the sums are exactly 15.

Solution

MagicSquareTest.jaav

  
   import java.util.Scanner;
  
   public class MagicSquareTest {
  
      
       public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
           int rows = 3, cols = 3;
           int a[][] = new int[rows][cols];
           System.out.println(\"Enter matrix elements;\");
           for(int i=0; i<rows; i++){
               for(int j=0; j<cols; j++){
                   a[i][j] = scan.nextInt();
               }
           }
           System.out.println(\"Entered Matrix is:\");
           for(int i=0; i<rows; i++){
               for(int j=0; j<cols; j++){
                   System.out.print(a[i][j] +\" \");
               }
               System.out.println();
           }
           boolean result = magicSquare(a);
           if(result){
               System.out.println(\"Given matrix is magic square\");
           }
           else{
               System.out.println(\"Given matrix is not magic square\");
           }
       }
       public static boolean magicSquare(int a[][]){
           int rowSum[] = new int[a.length];
           int colSum[] = new int[a.length];
           int diagonalSum1 = 0;
           int diagonalSum2 = 0;
                 
           for(int i=0; i<a.length; i++){
               for(int j=0; j<a[i].length; j++){
                   rowSum[i] = rowSum[i]+a[i][j];
               }
           }
           for(int i=0; i<rowSum.length; i++){
               System.out.println(\"Row \"+i+\" sum is \"+rowSum[i]);
           }
           for(int i=0; i<a.length; i++){
               for(int j=0; j<a[i].length; j++){
                   colSum[i] = colSum[i]+a[j][i];
               }
           }
           for(int i=0; i<colSum.length; i++){
               System.out.println(\"Column \"+i+\" sum is \"+colSum[i]);
           }
             
           for(int i=0; i<a.length; i++){
               diagonalSum1 = diagonalSum1+a[i][i];
           }
           for(int i=0; i<a.length; i++){
               diagonalSum2 = diagonalSum2+a[i][a.length-1-i];
           }
           System.out.println(\"Diagonal Sum1: \"+diagonalSum1);
           System.out.println(\"Diagonal Sum2: \"+diagonalSum2);
           for(int i=0; i<rowSum.length-1; i++){
               if(rowSum[i] != rowSum[i+1])
                   return false;
           }
           for(int i=0; i<colSum.length-1; i++){
               if(colSum[i] != colSum[i+1])
                   return false;
           }
           if(diagonalSum1 != diagonalSum2)
               return false;
             
           return true;
           }
  
   }

Output:

Enter matrix elements;
2 7 6 9 5 1 4 3 8
Entered Matrix is:
2 7 6
9 5 1
4 3 8
Row 0 sum is 15
Row 1 sum is 15
Row 2 sum is 15
Column 0 sum is 15
Column 1 sum is 15
Column 2 sum is 15
Diagonal Sum1: 15
Diagonal Sum2: 15
Given matrix is magic square

Create the following program using java, or anything to compare what i have been doing. Thanks Write a program that accepts 9 values from the user in the range
Create the following program using java, or anything to compare what i have been doing. Thanks Write a program that accepts 9 values from the user in the range

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site