Practice Programs on Arrays and Matrices

1. One-Dimensional Array practice program-1

This program stores elements in an array and displays them. It uses a for loop to access and print each array element.

import java.util.Scanner;

public class PrintArray {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int[] arr = new int[n];

        System.out.println("Enter array elements:");

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("Array Elements:");

        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }

        sc.close();
    }
}
Input and Output:
Enter the size of array: 5
Enter array elements:
10
20
30
40
50
Array Elements:
10 20 30 40 50 Code language: PHP (php)

2. One-Dimensional Array practice program-2

This program calculates the sum of all elements in an array. It traverses the array and adds each element to a sum variable.

import java.util.Scanner;

public class SumArray {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter array size: ");
        int n = sc.nextInt();

        int[] arr = new int[n];
        int sum = 0;

        System.out.println("Enter elements:");

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            sum += arr[i];
        }

        System.out.println("Sum = " + sum);

        sc.close();
    }
}
Input and Output:
Enter array size: 4
Enter elements:
5
10
15
20
Sum = 50Code language: PHP (php)

3. One-Dimensional Array practice program-3

This program finds the largest element in an array. It compares every element with the current largest value.

import java.util.Scanner;

public class LargestArray {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter array size: ");
        int n = sc.nextInt();

        int[] arr = new int[n];

        System.out.println("Enter elements:");

        for (int i = 0; i < n; i++)
            arr[i] = sc.nextInt();

        int largest = arr[0];

        for (int i = 1; i < n; i++) {

            if (arr[i] > largest)
                largest = arr[i];
        }

        System.out.println("Largest Element = " + largest);

        sc.close();
    }
}
Input and Output:
Enter array size: 4
Enter elements:
10
50
30
20
Largest Element = 50Code language: PHP (php)

4. One-Dimensional Array practice program-4

This program searches for an element in an array. It displays whether the entered element is found or not.

import java.util.Scanner;

public class SearchArray {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[] arr = {10,20,30,40,50};

        System.out.print("Enter element to search: ");
        int key = sc.nextInt();

        boolean found = false;

        for(int num : arr){

            if(num == key){
                found = true;
                break;
            }
        }

        if(found)
            System.out.println("Element Found");
        else
            System.out.println("Element Not Found");

        sc.close();
    }
}
Input and Output:
Enter element to search: 20
Element Found

5. Two-Dimensional Array practice program-1

This program adds two matrices of the same size. Each corresponding element is added and stored in the result matrix.

import java.util.Scanner;

public class MatrixAddition {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[][] A = new int[2][2];
        int[][] B = new int[2][2];
        int[][] C = new int[2][2];

        System.out.println("Enter First Matrix:");

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                A[i][j]=sc.nextInt();

        System.out.println("Enter Second Matrix:");

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                B[i][j]=sc.nextInt();

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                C[i][j]=A[i][j]+B[i][j];

        System.out.println("Result Matrix:");

        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++)
                System.out.print(C[i][j]+" ");
            System.out.println();
        }

        sc.close();
    }
}
Input and Output:
Enter First Matrix:
1 2
3 4
Enter Second Matrix:
5 6
7 8
Result Matrix:
6 8 
10 12 

6. Two-Dimensional Array practice program-2

This program subtracts one matrix from another. Each corresponding element is subtracted to produce the result matrix.

import java.util.Scanner;

public class MatrixSubtraction {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[][] A={{8,7},{6,5}};
        int[][] B={{4,3},{2,1}};

        System.out.println("Result Matrix:");

        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                System.out.print((A[i][j]-B[i][j])+" ");
            }
            System.out.println();
        }

        sc.close();
    }
}
Output:
Result Matrix:
4 4 
4 4 

7. Two-Dimensional Array practice program-3

This program multiplies two matrices. It uses three nested loops to compute the matrix product.

import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {

        int[][] A={{1,2},{3,4}};
        int[][] B={{5,6},{7,8}};
        int[][] C=new int[2][2];

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                for(int k=0;k<2;k++)
                    C[i][j]+=A[i][k]*B[k][j];

        System.out.println("Product Matrix:");

        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++)
                System.out.print(C[i][j]+" ");
            System.out.println();
        }
    }
}
Output:
Product Matrix:
19 22 
43 50 

8. Two-Dimensional Array practice program-4

This program finds the transpose of a matrix. Rows become columns and columns become rows.

public class MatrixTranspose {

    public static void main(String[] args) {

        int[][] A={{1,2,3},{4,5,6}};

        System.out.println("Transpose:");

        for(int i=0;i<3;i++){

            for(int j=0;j<2;j++){

                System.out.print(A[j][i]+" ");
            }

            System.out.println();
        }
    }
}
Output:
Transpose:
1 4 
2 5 
3 6 

9. Two-Dimensional Array practice program-5

This program calculates the sum of all elements in a matrix. It traverses every row and column using nested loops.

public class MatrixSum {

    public static void main(String[] args) {

        int[][] matrix={{1,2},{3,4}};

        int sum=0;

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                sum+=matrix[i][j];

        System.out.println("Sum = "+sum);
    }
}
Output:
Sum = 10

10. Two-Dimensional Array practice program-6

This program finds the largest element in a matrix. It compares every element with the current maximum value.

public class LargestMatrix {

    public static void main(String[] args) {

        int[][] matrix={{10,35},{45,22}};

        int largest=matrix[0][0];

        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                if(matrix[i][j]>largest)
                    largest=matrix[i][j];

        System.out.println("Largest Element = "+largest);
    }
}
Output:
Largest Element = 45
Scroll to Top