Java Programs

TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 29: Write a Java program to print the lower triangular matrix of the entered matrix.

LowerTriangle.java

import java.util.Scanner;

public class  LowerTriangle
{
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		
		//Input size of the matrix
		//row - number of rows in the matrix.
		//col - number of columns in the matrix.
		
		System.out.println("Enter the number of rows");
		int row = s.nextInt();
		System.out.println("Enter the number of columns");
		int col = s.nextInt();
		
		//Input matrix
		int matrix[][] = new int[row][col];

		System.out.println("Enter the matrix");
		for(int i=0; i < row;i++)
		{
			for(int j=0; j < col;j++)
			{
				System.out.print("Enter "+i+j+" element: ");
				matrix[i][j] = s.nextInt();
			}
		}
		
		System.out.println("Lower triangular matrix");
		for(int i=0; i < row;i++)
		{
			for(int j=0; j < col;j++)
			{
				if(j>i)
				{
					System.out.print(0+" ");
				}
				else
				{
					System.out.print(matrix[i][j]+" ");
				}
			}
			System.out.println();
		}
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.