Problem Statement
Given a matrix having ‘r’ rows and ‘c’ columns, you have to print the upper triangular matrix corresponding to the given matrix.

Algorithm
- Declare a 2D matrix of size ‘row’ x ‘col’ as ‘a[row][col]’.
- Run a nested for loop from ‘i’ = 0 to ‘i’ < ‘row’ and ‘j’=0 to ‘j’ < col
- Mark the entries provided by the user as input as a[0][0], a[0][1], a[0][2]....
- Again run a nested loop from ‘i’ = 0 to ‘i’ < ‘row’ and ‘j’=0 to ‘j’ < col
- Inside this loop check the following conditions:
- If column number >= row number (‘i’> ‘j’ )
- Then print the element ‘a[i][j]’
- If column number < row number.
- Then, print zero.
Example

Code
/* C program to find upper triangular matrix */
#include<stdio.h>
void UpperTriangularMatrix(int row, int col)
{
int a[row][col];
printf("\nEnter the Elements \n");
// Nested loop to input array elements from the user
for(int i = 0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Required Matrix : ");
// Nested loop to print upper triangular matrix
for(int i = 0; i<row; i++)
{
printf("\n");
for(int j = 0; j<col; j++)
{
// Check if column >= row
if(j >= i)
printf(" %d ", a[i][j]);
else
printf(" 0 ");
}
}
}
int main()
{
int row, col;
// Input number of rows and columns by the user
printf("\nEnter Number of rows : ");
scanf("%d", &row);
printf("\nEnter Number of columns : ");
scanf("%d", &col);
// Calling Upper triangular Matrix function
UpperTriangularMatrix(row,col);
return 0;
}

You can also try this code with Online C Compiler
Run CodeProgram Explanation
In the above C program to find upper triangular matrix, we have asked the user to input the number of rows and columns ( size of two-dimensional matrix ), passing these to a user-defined function created to print the upper triangular matrix.
In this function, we created a two-dimensional array of the size of row * col ( a[row][col] ). In next following statements, we used a for loop to iterate through each cell of the array a[row][col]. Conditions in the for loop ( i < row and j < col ) ensure that the compiler will not exceed the maximum limit. The scanf statement inside these for loops asks the user to enter the elements in the matrix such as a[0][0], a[0][1], a[0][2]....
for(int i = 0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
scanf("%d", &a[i][j]);
}
}

You can also try this code with Online C Compiler
Run Code
In the next line, we have used one more nested for loop to find the upper triangular matrix of the above input matrix.
The condition, if(j >= i) is used to check if the column number is greater than the row number, if this condition is true we will directly print the element on position ( i,j ) i.e, printf(“%d”, a[i][j]), otherwise we will print zero i.e, printf(“ 0 “);
This condition is false for all the elements present below the principal diagonal.
for(int i = 0; i<row; i++)
{
printf("\n");
for(int j = 0; j<col; j++)
{
if(j >= i)
printf(" %d ", a[i][j]);
else
printf(" 0 ");
}
}

You can also try this code with Online C Compiler
Run CodeOutput
Example of 2 x 2 matrix:

Example of 4 x 4 matrix:

You can practice it on online editor for good practice.
Time and Space Complexity

Time Complexity: O(R x C)
Space Complexity: O(R x C), for the size of Two Dimensional Array
Where R = number of rows
And C = number of columns
Frequently Asked Questions
What is a matrix?
A matrix is an array or set of numbers arranged in rows and columns in the form of a rectangular array. The numbers are known as the entities or elements of the matrix.
What is an upper triangular matrix?
The upper triangular matrix (also known as the right triangular matrix) is a type of triangular matrix in which all the entries below the principal diagonal are zero.
What are the different types of matrices?
Upper Triangular Matrix, Lower Triangular Matrix, Diagonal Matrix, Square Matrix, Symmetrix Matrix, Row Matrix, Column Matrix, Null Matrix, Unit Matrix, and Skew-Symmetric Matrix are the different types of matrices.
What are the applications of a matrix?
Matrices are widely used in our daily life. A few of the real-life uses of matrices are
- Business and Economics for the study of trends and business models.
- In hospitals for medical images, MRI (Magnetic resonance imaging) scans, etc.
- Graphic software like Adobe Photoshop uses matrices for doing the linear transformation for representing images.
- In 3D Games for reconstructing and modifying objects in 3D space.
- For making animations more and more precise and accurate.
How to print the upper triangle matrix in C?
To print an upper triangular matrix in C, you can use a nested loop to iterate over the rows and columns of the matrix and print only the elements that are above or on the diagonal.
Conclusion
In this article, we see the implementation of the C program to find the upper triangular matrix. We had also seen the output of the written program on some random input.
If you want to learn more about C programs, visit the given links below: