ch
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

前往频道在 Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

显示更多
2 018
订阅者
无数据24 小时
-77
-4030
帖子存档
💻 Wave Form Traversal of Matrix
import java.util.Scanner;

public class WaveFormTraversal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int numRows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int numCols = scanner.nextInt();

        int[][] matrix = new int[numRows][numCols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        System.out.println("Wave form traversal of the matrix:");

        for (int j = 0; j < numCols; j++) {
            if (j % 2 == 0) {
                // Even column, traverse top to bottom
                for (int i = 0; i < numRows; i++) {
                    System.out.print(matrix[i][j] + " ");
                }
            } else {
                // Odd column, traverse bottom to top
                for (int i = numRows - 1; i >= 0; i--) {
                    System.out.print(matrix[i][j] + " ");
                }
            }
        }
        System.out.println(); // Add a newline at the end

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 4
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Input: 10
Input: 11
Input: 12
Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix:
Wave form traversal of the matrix:
1 5 9 10 6 2 3 7 11 12 8 4

💻 Spiral Order Traversal of Matrix
import java.util.Scanner;

public class SpiralMatrixTraversal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int rows = scanner.nextInt();
        int cols = scanner.nextInt();

        int[][] matrix = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int topRow = 0, bottomRow = rows - 1;
        int leftCol = 0, rightCol = cols - 1;

        while (topRow <= bottomRow && leftCol <= rightCol) {

            // Traverse top row
            for (int i = leftCol; i <= rightCol; i++) {
                System.out.print(matrix[topRow][i] + " ");
            }
            topRow++;

            // Traverse right column
            for (int i = topRow; i <= bottomRow; i++) {
                System.out.print(matrix[i][rightCol] + " ");
            }
            rightCol--;

            // Traverse bottom row
            if (topRow <= bottomRow) { //check before printing to avoid duplicates when it is a single row matrix
                for (int i = rightCol; i >= leftCol; i--) {
                    System.out.print(matrix[bottomRow][i] + " ");
                }
                bottomRow--;
            }


            // Traverse left column
            if (leftCol <= rightCol) { //check before printing to avoid duplicates when it is a single column matrix
                for (int i = bottomRow; i >= topRow; i--) {
                    System.out.print(matrix[i][leftCol] + " ");
                }
                leftCol++;
            }

        }

        scanner.close();
    }
}
📤 Output:
Input: 3 4
1 2 3 4
5 6 7 8
9 10 11 12
Output: 1 2 3 4 8 12 11 10 9 5 6 7
Input: 4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Input: 1 4
1 2 3 4
Output: 1 2 3 4
Input: 4 1
1
2
3
4
Output: 1 2 3 4
Input: 2 2
1 2
3 4
Output: 1 2 4 3
Input: 3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5

💻 Rotate Matrix by 90 Degrees
import java.util.Scanner;

public class RotateMatrix {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows/columns for the square matrix: ");
        int n = scanner.nextInt();

        int[][] matrix = new int[n][n];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        rotateMatrix(matrix);

        System.out.println("Rotated Matrix:");
        printMatrix(matrix);

        scanner.close();
    }

    public static void rotateMatrix(int[][] matrix) {
        int n = matrix.length;

        // Transpose the matrix
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

        // Reverse each row
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n / 2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][n - 1 - j];
                matrix[i][n - 1 - j] = temp;
            }
        }
    }

    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
📤 Output:
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Output: Rotated Matrix:
7 4 1
8 5 2
9 6 3

💻 Matrix Multiplication Validation
import java.util.Scanner;

public class MatrixMultiplicationValidation {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows for matrix A: ");
        int rowsA = scanner.nextInt();

        System.out.print("Enter the number of columns for matrix A: ");
        int colsA = scanner.nextInt();

        System.out.print("Enter the number of rows for matrix B: ");
        int rowsB = scanner.nextInt();

        System.out.print("Enter the number of columns for matrix B: ");
        int colsB = scanner.nextInt();

        // Condition for matrix multiplication
        if (colsA == rowsB) {
            System.out.println("Matrix multiplication is possible.");
        } else {
            System.out.println("Matrix multiplication is not possible.");
        }

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 2
Input: 2
Input: 4
Output: Matrix multiplication is possible.

Input: 3
Input: 2
Input: 3
Input: 4
Output: Matrix multiplication is not possible.

💻 Find Minimum Element in Matrix
import java.util.Scanner;

public class MinimumElementInMatrix {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();

        int[][] matrix = new int[rows][cols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int minElement = matrix[0][0]; // Assume first element is minimum initially

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i][j] < minElement) {
                    minElement = matrix[i][j];
                }
            }
        }

        System.out.println("Minimum element in the matrix: " + minElement);

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix:
Minimum element in the matrix: 1

Input: 2
Input: 4
Input: 10
Input: 20
Input: 30
Input: 40
Input: 5
Input: 15
Input: 25
Input: 35
Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix:
Minimum element in the matrix: 5

Input: 1
Input: 5
Input: 5
Input: 4
Input: 3
Input: 2
Input: 1
Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix:
Minimum element in the matrix: 1

💻 Find Maximum Element in Matrix
import java.util.Scanner;

public class MaxElementInMatrix {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        int[][] matrix = new int[rows][columns];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int maxElement = matrix[0][0]; // Initialize maxElement with the first element

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (matrix[i][j] > maxElement) {
                    maxElement = matrix[i][j];
                }
            }
        }

        System.out.println("Maximum element in the matrix: " + maxElement);

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Output: Maximum element in the matrix: 6

💻 Find Row Sum and Column Sum
import java.util.Scanner;

public class RowColumnSum {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();

        int[][] matrix = new int[rows][cols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        // Calculate and print row sums
        System.out.println("Row Sums:");
        for (int i = 0; i < rows; i++) {
            int rowSum = 0;
            for (int j = 0; j < cols; j++) {
                rowSum += matrix[i][j];
            }
            System.out.println("Row " + (i + 1) + ": " + rowSum);
        }

        // Calculate and print column sums
        System.out.println("Column Sums:");
        for (int j = 0; j < cols; j++) {
            int colSum = 0;
            for (int i = 0; i < rows; i++) {
                colSum += matrix[i][j];
            }
            System.out.println("Column " + (j + 1) + ": " + colSum);
        }

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix:
Row Sums:
Row 1: 6
Row 2: 15
Column Sums:
Column 1: 5
Column 2: 7
Column 3: 9

💻 Check if Matrix is Identity Matrix
import java.util.Scanner;

public class IdentityMatrixChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows (and columns) for the square matrix: ");
        int rows = scanner.nextInt();

        int[][] matrix = new int[rows][rows];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        boolean isIdentity = true;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                if (i == j) {
                    if (matrix[i][j] != 1) {
                        isIdentity = false;
                        break;
                    }
                } else {
                    if (matrix[i][j] != 0) {
                        isIdentity = false;
                        break;
                    }
                }
            }
            if (!isIdentity) {
                break;
            }
        }

        if (isIdentity) {
            System.out.println("The matrix is an identity matrix.");
        } else {
            System.out.println("The matrix is not an identity matrix.");
        }

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 1
Input: 0
Input: 0
Input: 0
Input: 1
Input: 0
Input: 0
Input: 0
Input: 1
Output: The matrix is an identity matrix.

Input: 2
Input: 1
Input: 2
Input: 0
Input: 1
Output: The matrix is not an identity matrix.

Input: 2
Input: 1
Input: 0
Input: 0
Input: 1
Output: The matrix is an identity matrix.

💻 Check if Matrix is Symmetric
import java.util.Scanner;

public class SymmetricMatrixChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();

        if (rows != cols) {
            System.out.println("Matrix is not square, hence not symmetric.");
            scanner.close();
            return;
        }

        int[][] matrix = new int[rows][cols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        boolean isSymmetric = true;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i][j] != matrix[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.");
        }

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 3
Input: 1
Input: 2
Input: 3
Input: 2
Input: 4
Input: 5
Input: 3
Input: 5
Input: 6
Output: The matrix is symmetric.

Input: 2
Input: 3
Output: Matrix is not square, hence not symmetric.

Input: 2
Input: 2
Input: 1
Input: 2
Input: 3
Input: 4
Output: The matrix is not symmetric.

💻 Sum of Secondary Diagonal Elements
import java.util.Scanner;

public class SumOfSecondaryDiagonal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows (and columns, assuming it's a square matrix): ");
        int numberOfRows = scanner.nextInt();

        int[][] matrix = new int[numberOfRows][numberOfRows];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < numberOfRows; i++) {
            for (int j = 0; j < numberOfRows; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int sumOfSecondaryDiagonal = 0;
        for (int i = 0; i < numberOfRows; i++) {
            sumOfSecondaryDiagonal += matrix[i][numberOfRows - 1 - i];
        }

        System.out.println("Sum of secondary diagonal elements: " + sumOfSecondaryDiagonal);

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 1 2 3
Input: 4 5 6
Input: 7 8 9
Output: Sum of secondary diagonal elements: 15

💻 Sum of Main Diagonal Elements
import java.util.Scanner;

public class SumOfMainDiagonal {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows (and columns, since it's a square matrix): ");
        int rows = scanner.nextInt();

        int[][] matrix = new int[rows][rows];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int sum = 0;
        for (int i = 0; i < rows; i++) {
            sum += matrix[i][i]; // Add the element at the main diagonal (where row index equals column index)
        }

        System.out.println("Sum of the main diagonal elements: " + sum);

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 1 2 3
Input: 4 5 6
Input: 7 8 9
Output: Enter the number of rows (and columns, since it's a square matrix): Enter the elements of the matrix:
Sum of the main diagonal elements: 15

💻 Transpose of a Matrix
import java.util.Scanner;

public class MatrixTranspose {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();

        int[][] matrix = new int[rows][cols];

        System.out.println("Enter the elements of the matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int[][] transpose = new int[cols][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }

        System.out.println("Transpose of the matrix:");
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                System.out.print(transpose[i][j] + " ");
            }
            System.out.println();
        }

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Output: Transpose of the matrix:
Output: 1 4
Output: 2 5
Output: 3 6

💻 Multiply Two Matrices
import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows for the first matrix: ");
        int rows1 = scanner.nextInt();
        System.out.print("Enter the number of columns for the first matrix: ");
        int cols1 = scanner.nextInt();

        System.out.print("Enter the number of rows for the second matrix: ");
        int rows2 = scanner.nextInt();
        System.out.print("Enter the number of columns for the second matrix: ");
        int cols2 = scanner.nextInt();

        if (cols1 != rows2) {
            System.out.println("Matrix multiplication is not possible. Number of columns in the first matrix must be equal to the number of rows in the second matrix.");
            scanner.close();
            return;
        }

        int[][] matrix1 = new int[rows1][cols1];
        int[][] matrix2 = new int[rows2][cols2];
        int[][] resultMatrix = new int[rows1][cols2];

        System.out.println("Enter the elements for the first matrix:");
        for (int i = 0; i < rows1; i++) {
            for (int j = 0; j < cols1; j++) {
                matrix1[i][j] = scanner.nextInt();
            }
        }

        System.out.println("Enter the elements for the second matrix:");
        for (int i = 0; i < rows2; i++) {
            for (int j = 0; j < cols2; j++) {
                matrix2[i][j] = scanner.nextInt();
            }
        }

        // Matrix Multiplication Logic
        for (int i = 0; i < rows1; i++) {
            for (int j = 0; j < cols2; j++) {
                for (int k = 0; k < cols1; k++) {
                    resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j];
                }
            }
        }

        System.out.println("Resultant Matrix after multiplication:");
        for (int i = 0; i < rows1; i++) {
            for (int j = 0; j < cols2; j++) {
                System.out.print(resultMatrix[i][j] + " ");
            }
            System.out.println();
        }

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 3
Input: 2
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Input: 10
Input: 11
Input: 12
Output: Enter the number of rows for the first matrix: Enter the number of columns for the first matrix: Enter the number of rows for the second matrix: Enter the number of columns for the second matrix: Enter the elements for the first matrix:
Enter the elements for the second matrix:
Resultant Matrix after multiplication:
58 64
139 154

💻 Subtract Two Matrices
import java.util.Scanner;

public class SubtractTwoMatrices {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        int[][] matrixA = new int[rows][columns];
        int[][] matrixB = new int[rows][columns];
        int[][] differenceMatrix = new int[rows][columns];

        System.out.println("Enter elements for the first matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrixA[i][j] = scanner.nextInt();
            }
        }

        System.out.println("Enter elements for the second matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrixB[i][j] = scanner.nextInt();
            }
        }

        // Subtract the matrices
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                differenceMatrix[i][j] = matrixA[i][j] - matrixB[i][j];
            }
        }

        System.out.println("The difference of the two matrices is:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(differenceMatrix[i][j] + " ");
            }
            System.out.println();
        }

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Input: 10
Input: 11
Input: 12
Output: Enter the number of rows: Enter the number of columns: Enter elements for the first matrix:
Enter elements for the second matrix:
The difference of the two matrices is:
-6 -6 -6
-6 -6 -6

💻 Add Two Matrices
import java.util.Scanner;

public class AddTwoMatrices {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        int[][] matrixA = new int[rows][columns];
        int[][] matrixB = new int[rows][columns];
        int[][] sumMatrix = new int[rows][columns];

        System.out.println("Enter elements for Matrix A:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrixA[i][j] = scanner.nextInt();
            }
        }

        System.out.println("Enter elements for Matrix B:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrixB[i][j] = scanner.nextInt();
            }
        }

        // Adding the two matrices
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                sumMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
            }
        }

        System.out.println("Sum of the two matrices:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(sumMatrix[i][j] + " ");
            }
            System.out.println();
        }

        scanner.close();
    }
}
📤 Output:
Input: 2
Input: 3
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Input: 7
Input: 8
Input: 9
Input: 10
Input: 11
Input: 12
Output: Enter the number of rows: Enter the number of columns: Enter elements for Matrix A:
Enter elements for Matrix B:
Sum of the two matrices:
8 10 12
14 16 18

Arrays - 2D

💻 Count Occurrences of Element in Array
import java.util.Scanner;

public class CountElementOccurrences {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        int[] myArray = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            myArray[i] = scanner.nextInt();
        }

        System.out.print("Enter the element to count: ");
        int elementToCount = scanner.nextInt();

        int count = 0;
        for (int i = 0; i < size; i++) {
            if (myArray[i] == elementToCount) {
                count++;
            }
        }

        System.out.println("The element " + elementToCount + " appears " + count + " times in the array.");

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 2
Input: 3
Input: 2
Input: 2
Output: Enter the size of the array: Enter the elements of the array:
Enter the element to count: The element 2 appears 3 times in the array.

💻 Find All Prime Numbers in Array
import java.util.Scanner;

public class PrimeNumbersInArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int n = scanner.nextInt();

        int[] arr = new int[n];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        System.out.println("Prime numbers in the array are:");
        for (int i = 0; i < n; i++) {
            if (isPrime(arr[i])) {
                System.out.print(arr[i] + " ");
            }
        }
        System.out.println();

        scanner.close();
    }

    static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
📤 Output:
Input: 5
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Output: Enter the number of elements in the array: Enter the elements of the array:
Prime numbers in the array are:
2 3 5

💻 Split Array into Even and Odd Numbers
import java.util.ArrayList;
import java.util.Scanner;

public class SplitArrayEvenOdd {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        int[] inputArray = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            inputArray[i] = scanner.nextInt();
        }

        ArrayList<Integer> evenNumbers = new ArrayList<>();
        ArrayList<Integer> oddNumbers = new ArrayList<>();

        for (int number : inputArray) {
            if (number % 2 == 0) {
                evenNumbers.add(number);
            } else {
                oddNumbers.add(number);
            }
        }

        System.out.print("Even numbers: ");
        for (int i = 0; i < evenNumbers.size(); i++) {
            System.out.print(evenNumbers.get(i) + " ");
        }
        System.out.println();

        System.out.print("Odd numbers: ");
        for (int i = 0; i < oddNumbers.size(); i++) {
            System.out.print(oddNumbers.get(i) + " ");
        }
        System.out.println();

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the size of the array: Enter the elements of the array:
Even numbers: 2 4
Odd numbers: 1 3 5

💻 Find Mode of Array
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class FindArrayMode {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] numbers = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            numbers[i] = scanner.nextInt();
        }

        int mode = findMode(numbers);

        System.out.println("Mode of the array is: " + mode);

        scanner.close();
    }

    public static int findMode(int[] array) {
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        for (int number : array) {
            frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1);
        }

        int mode = -1;
        int maxFrequency = 0;

        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue() > maxFrequency) {
                mode = entry.getKey();
                maxFrequency = entry.getValue();
            }
        }

        return mode;
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 2
Input: 3
Input: 2
Output: Enter the size of the array: Enter the elements of the array:
Mode of the array is: 2