Array-promo
Matrix-promo

Question

Print elements below the left diagonal of the integer matrix.

				
					Enter size of array:
4
Enter Elements in Array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Original Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
Elements Below the Left diagonal of Matrix:
        
5       
9 10     
13 14 15   
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class ElementsBelowLeftDiagonalMatrix
{
    public static void main()
    {
        int[ ][ ] arr;
        int m;
        int i,j;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter size of array:");
        m=sc.nextInt();        
        arr=new int[m][m];
        System.out.println("Enter Elements in Array:");
        for(i=0;i< m;i++)
        {
            for(j=0;j< m;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
        System.out.println("Original Matrix:");
        for(i=0;i< m;i++)
        {
            for(j=0;j< m;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }

        System.out.println("Elements Below the Left diagonal of Matrix:");
        for(i=0;i< m;i++)
        {
            for(j=0;j< m;j++)
            {
                if(j< i)
                {
                    System.out.print(arr[i][j]+" "); 
                }
                else
                {
                    System.out.print("  ");
                }

            }
            System.out.println();
        }
    }
}
				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *