Upload files to "OOP/Java/Lab/Week4"
This commit is contained in:
parent
f7cc8cb99a
commit
48775a2c5e
55
OOP/Java/Lab/Week4/MagicSquareCheck.java
Normal file
55
OOP/Java/Lab/Week4/MagicSquareCheck.java
Normal file
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
38
OOP/Java/Lab/Week4/NonDiagEle.java
Normal file
38
OOP/Java/Lab/Week4/NonDiagEle.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
34
OOP/Java/Lab/Week4/PrincipEleSum.java
Normal file
34
OOP/Java/Lab/Week4/PrincipEleSum.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
41
OOP/Java/Lab/Week4/SymmMatrixCheck.java
Normal file
41
OOP/Java/Lab/Week4/SymmMatrixCheck.java
Normal file
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user