Java Program to display upper triangular matrix
In this tutorial, we will learn how to display the upper triangular matrix. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Below is the pictorial representation for the same.
Input: Enter the Matrix Elements:
1 2 3
4 5 6
7 8 9
Output: The upper triangular matrix is:
1 2 3
0 5 6
0 0 9
Program 1: Display Upper Triangular Matrix
In the below program we will see how to display the upper triangular matrix when values are user-defined. Here, we will ask the user to enter the matrix elements and then we will display only those elements of the matrix whose row's number is greater than the column's number.
Algorithm
- Start
- Declare variables to store the number of rows and columns.
- Ask the user to initialize the rows and columns.
- Check if the number of rows and columns are equal or not.
- If not equal, then display a message that the number of rows and columns should be equal.
- If equal, then declare a matrix.
- Ask the user to initialize the matrix elements.
- Print the original matrix.
- Call a method to display the upper triangular matrix.
- Use a loop to iterate over the elements.
- Assign 0 to the elements whose row's number is greater than the column's number.
- Print the resultant matrix.
- Stop.
Below is the code for the same.
// Java Program to print the upper triangular matrix
import java.util.*;
public class Main
{
// Print the matrix
public static void printMatrix(int[][] arr)
{
int m = arr.length; //For Rows
int n = arr[0].length; //For columns
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//Display the upper triangular matrix
public static void upperTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;
}
else
{
// looping over the whole matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i > j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Upper Triangular Matrix is : ");
// printing the upper triangular matrix
printMatrix(arr);
}
}
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
int m,n; //Declare variables for rows and columns
System.out.println("Enter the number of rows: ");
m=sc.nextInt();
System.out.println("Enter the number of columns: ");
n=sc.nextInt();
System.out.println("Enter the matrix elements: ");
int arr[][] = new int[m][n]; //Declare the matrix
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt(); //Initialize the matrix
}
}
//Print Original Matrix
System.out.println( "Original Matrix is : ");
printMatrix(arr);
// calling to display the upper triangular matrix
upperTriangularMatrix(arr);
}
}
Enter the number of rows: 3
Enter the number of columns: 3
Enter the matrix Elements: 1 2 8 7 6 5 4 3 9
Original Matrix is :
1 2 8
7 6 5
4 3 9
Upper Triangular Matrix is :
1 2 8
0 6 5
0 0 9
Program 2: Display Upper Triangular Matrix
In the below program, we will see how to display the upper triangular matrix when values are pre-defined. Here, the elements for the matrix are pre-defined in the program. So, we will display only those elements of the matrix whose row's number is greater than the column's number.
Algorithm
- Start
- Declare variables to store the number of rows and columns.
- Initialize the rows and columns.
- Check if the number of rows and columns are equal or not.
- If not equal, then display a message that the number of rows and columns should be equal.
- If equal, then declare a matrix.
- Initialize the matrix elements.
- Print the original matrix.
- Call a method to display the upper triangular matrix.
- Use a loop to iterate over the elements.
- Assign 0 to the elements whose row's number is greater than the column's number.
- Print the resultant matrix.
- Stop.
Below is the code for the same.
// Java Program to print the upper triangular matrix
import java.io.*;
public class Main
{
// Print the matrix
public static void printMatrix(int[][] arr)
{
int m = arr.length; //For Rows
int n = arr[0].length; //For columns
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//Display the upper triangular matrix
public static void upperTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;
}
else
{
// looping over the whole matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i > j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Upper Triangular Matrix is : ");
// printing the upper triangular matrix
printMatrix(arr);
}
}
public static void main(String[] args)
{
int arr[][] = { { 8, 7, 6 }, { 4, 2, 5 }, { 7, 9, 8 } };
//Print Original Matrix
System.out.println( "Original Matrix is : ");
printMatrix(arr);
// calling to display the upper triangular matrix
upperTriangularMatrix(arr);
}
}
Original Matrix is :
8 7 6
4 2 5
7 9 8
Upper Triangular Matrix is :
8 7 6
0 2 5
0 0 8