Elements below Left Diagonal in a Square Matrix

Write a program in Java to allow the user to create a square matrix of integer type.

The value of ‘m’ is entered by the user. Ensure that 3 < m < 10.

For an invalid range, display a suitable error message.

Now allow the user to fill this matrix with integers.

Finally, once the array is full, display the elements that fall below the left diagonal.

import java.io.*;
class BelowLeft{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("M = ");
        int m = Integer.parseInt(br.readLine());
        if(m < 4 || m > 9){
            System.out.println("Invalid range!");
            return;
        }
        int a[][] = new int[m][m];
        System.out.print("Enter integers:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                a[i][j] = Integer.parseInt(br.readLine());
            }
        }
        System.out.println("Original Matrix:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                System.out.print(a[i][j] + "\t");;
            }
            System.out.println();
        }
        System.out.println("Elements below left diagonal:");
        for(int i = 1; i < m; i++){
            for(int j = 0; j < i; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

2 thoughts on “Elements below Left Diagonal in a Square Matrix

  1. Sir,a program is given which states that:
    Write a program to create a matrix a[][] of order (m × n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10 .Now fill each row of the matrix uniformly by 1 or 0 alternately.
    Sample Input:
    M=3
    N=3
    Resultant matrix:
    1 1 1
    0 0 0
    1 1 1

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.