Table of contents
1.
Introduction
2.
What is an upper triangular matrix in C?
2.1.
Example of matrix
2.2.
Example of Upper triangular matrix
3.
Problem Statement
4.
Algorithm
4.1.
Example
5.
Code
5.1.
Program Explanation
5.2.
Output
5.3.
Time and Space Complexity
6.
Frequently Asked Questions
6.1.
What is a matrix?
6.2.
What is an upper triangular matrix?
6.3.
What are the different types of matrices?
6.4.
What are the applications of a matrix?
6.5.
How to print the upper triangle matrix in C?
7.
Conclusion
Last Updated: Mar 6, 2025
Easy

C Program to Find Upper Triangular Matrix

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article we will discuss a C program to find upper triangular matrix, briefly discus the approach used, and it’s time and space complexity. Before discussing the C program to find upper triangular matrix, Let’s first discuss what is meant by a matrix and an upper triangular matrix.

Upper Triangular Matrix in C

A matrix is an array or table of numbers arranged in rows and columns, usually denoted by a capital letter. 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.

Recommended Topic, Binary to Hex Converter and C Static Function.

What is an upper triangular matrix in C?

An upper triangular matrix is a special kind of square matrix in which all the elements below the main diagonal are zero. The main diagonal of an upper triangular matrix runs from the upper left corner of the matrix to the lower right corner.

The matrix has the form: 

a11 a12 a13 ... a1n
0   a22 a23 ... a2n
0   0   a33 ... a3n
.   .   .    ... …….
.   .   .    ... ……
.   .   .    ... …..
0   0   0   ... ann

 

where aij represents the element in row i and column j of the matrix.

This matrix is an upper triangular matrix because all the elements below the main diagonal (i.e., the entries with row index greater than column index) are zero. In this matrix, the main diagonal consists of the entries a11,a22,a33… ann.

Example of matrix

Example of Upper triangular matrix

Example of Upper triangular matrix

Problem Statement

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

Upper Triangular Problem Statement

Algorithm

  1. Declare a 2D matrix of size ‘row’ x ‘col’ as ‘a[row][col]’. 
     
  2. Run a nested for loop from ‘i’ = 0 to ‘i’ < ‘row’ and ‘j’=0 to ‘j’ < col  
     
  3. Mark the entries provided by the user as input as a[0][0], a[0][1], a[0][2]....
     
  4. Again run a nested loop from ‘i’ = 0 to ‘i’ < ‘row’ and ‘j’=0 to ‘j’ < col
     
  5. 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

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 Code

Program 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 Code

Output

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 matrixWe 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:

Live masterclass