diff --git a/OOP/Java/Lab/Week4/MagicSquareCheck.java b/OOP/Java/Lab/Week4/MagicSquareCheck.java new file mode 100644 index 0000000..8b365f2 --- /dev/null +++ b/OOP/Java/Lab/Week4/MagicSquareCheck.java @@ -0,0 +1,55 @@ +import java.util.Scanner; + +class MagicSquareCheck { + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the dimension of the square matrix: "); + int n = sc.nextInt(); + int a[][] = new int[n][n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + System.out.print( + "Enter the element at (" + i + "," + j + "): " + ); + a[i][j] = sc.nextInt(); + } + } + + // sum of the first row (to have a baseline sum) + int sum = 0; + for (int j = 0; j < n; j++) { + sum += a[0][j]; + } + + // rows, columns, and diagonals + boolean isMagicSquare = true; + int diag1 = 0, diag2 = 0; + + for (int i = 0; i < n; i++) { + int rowSum = 0, colSum = 0; + diag1 += a[i][i]; + diag2 += a[i][n - 1 - i]; + + for (int j = 0; j < n; j++) { + rowSum += a[i][j]; + colSum += a[j][i]; + } + + if (rowSum != sum || colSum != sum) { + isMagicSquare = false; + break; + } + } + + if (diag1 != sum || diag2 != sum) { + isMagicSquare = false; + } + + if (isMagicSquare) { + System.out.println("The matrix is a magic square."); + } else { + System.out.println("The matrix is not a magic square."); + } + } +} diff --git a/OOP/Java/Lab/Week4/NonDiagEle.java b/OOP/Java/Lab/Week4/NonDiagEle.java new file mode 100644 index 0000000..43af516 --- /dev/null +++ b/OOP/Java/Lab/Week4/NonDiagEle.java @@ -0,0 +1,38 @@ +import java.util.Scanner; + +class NonDiagEle { + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the dimension of the square matrix: "); + int n = sc.nextInt(); + int a[][] = new int[n][n]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + System.out.print( + "Enter the element at (" + i + "," + j + "): " + ); + a[i][j] = sc.nextInt(); + } + } + + System.out.println(); + int nonDiagSum = 0; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i != j && i + j != n - 1) { + System.out.print(a[i][j] + " "); + nonDiagSum += a[i][j]; + } else { + System.out.print("X "); + } + } + System.out.println(); + } + + System.out.println(); + System.out.println("Sum of Non-Diagonal Elements: " + nonDiagSum); + } +} diff --git a/OOP/Java/Lab/Week4/PrincipEleSum.java b/OOP/Java/Lab/Week4/PrincipEleSum.java new file mode 100644 index 0000000..275ce95 --- /dev/null +++ b/OOP/Java/Lab/Week4/PrincipEleSum.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +class PrincipEleSum { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the dimension of the square matrix: "); + int n = sc.nextInt(); + int a[][] = new int[n][n]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + System.out.print( + "Enter the element at (" + i + "," + j + "): " + ); + a[i][j] = sc.nextInt(); + } + } + + System.out.print("The Principal Elements are: [ "); + // Principal Element Printing and Sum + int sum = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i == j) { + System.out.print(a[i][j] + " "); + sum += a[i][j]; + } + } + } + System.out.println("]"); + System.out.println("Principal Element Sum: " + sum); + } +} diff --git a/OOP/Java/Lab/Week4/SymmMatrixCheck.java b/OOP/Java/Lab/Week4/SymmMatrixCheck.java new file mode 100644 index 0000000..9239731 --- /dev/null +++ b/OOP/Java/Lab/Week4/SymmMatrixCheck.java @@ -0,0 +1,41 @@ +import java.util.Scanner; + +class SymmMatrixCheck { + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number of rows in the matrix: "); + int m = sc.nextInt(); + System.out.print("Enter the number of colums in the matrix: "); + int n = sc.nextInt(); + + int a[][] = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + System.out.print( + "Enter the element at (" + i + "," + j + "): " + ); + a[i][j] = sc.nextInt(); + } + } + + boolean isSymmetric = true; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i != j && a[i][j] != a[j][i]) { + isSymmetric = false; + break; + } + } + if (!isSymmetric) { + break; + } + } + + if (isSymmetric) { + System.out.println("The matrix is symmetric."); + } else { + System.out.println("The matrix is not symmetric."); + } + } +}