Monday , December 18 2017
Home / C / Program to check Upper Triangular matrix

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which all the elements below the diagonal are zero. We consider a Matrix U with ‘i’ rows and ‘j’ columns, then mathematically we represent the upper triangular matrix as
upper triangular matrix formula

In another word strictly upper triangular matrix is an upper triangular matrix having 0’s along the diagonal as well, i.e., U(ij)=0 for i>=j.

UpperTriangularMatrix

 

  • Program in C

     

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int **m, o, i , j;
            printf("Enter the order of matrix:");             // the order of the matrix from user
            scanf("%d", &o);
            m = (int **)malloc(sizeof (int) * o);        // dynamically allocate memory to store entries 
            for (i = 0; i < o; i++)
            {
                    m[i] = (int *)malloc(sizeof (int) * o);
            }
            printf("Enter your inputs:n");           // get the input matrix from the user
            for (i = 0; i < o; i++)
            {
                   for (j = 0; j < o; j++)
                   {
                            scanf("%d", &m[i][j]);
                    }
              }
              for (i = 0; i < o; i++)             // check for upper triangulat matrix 
             {      
                    for (j = i - 1; j >= 0; j--)
                    {
                            if (m[i][j] != 0)
                            {
                                    printf("Not an upper triangular matrixn");
                                    exit(0);
                            }
                    }
            }
            printf("Given matrix is an upper triangular matrixn");            // print the output of upper matrix
            return 0;
      }
    

     Output:

    Output 1:

    Enter the order of matrix:3
    Enter your inputs:
    1  2 3
    0  4 5
    0  0 9
    Given matrix is an upper triangular matrix

     

    Output 2:

    Enter the order of matrix:3
    Enter your inputs:
    1  2 3
    0  4 5
    7  0 9
    Not an upper triangular matrix

  • Program in java

    import java.util.*;
    class UpperTriangularMatrix
    {
        public static void main(String args[])throws Exception
        {
            Scanner sy=new Scanner(System.in);
            System.out.print("Enter the order of the matrix : ");    //get order of matrix
            int n=sy.nextInt();
            int Arr[][]=new int[n][n];
             
    
            for(int i=0;i<n;i++)           // get Input in the matrix
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print("Enter an element : ");
                    Arr[i][j]=sy.nextInt();
                }
            }
     
    
            System.out.println("____________________");              // Print the matrix 
            System.out.println("The Matrix you enter is : ");
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(Arr[i][j]+"t");
                }
                System.out.println();
            }
            System.out.println("_____________________");
             
            int k=0;
             
            for(int i=0;i<n;i++)                         // Check the matrix is Upper Triangular Matrix or not
            {
                for(int j=0;j<i;j++)
                {
                    if(Arr[i][j]!=0) // All elements below the diagonal must be zero
                    {
                        p=1;
                        break;
                    }
                }
            }
             
            if(p==0)
                System.out.println("The matrix is an Upper Triangular Matrix");
            else
                System.out.println("The matrix is not an Upper Triangular Matrix");
        }
    }

     Output:

    Enter the order of the matrix : 4
    Enter an element : 1
    Enter an element : 2
    Enter an element : 3
    Enter an element : 4
    Enter an element : 0
    Enter an element : 6
    Enter an element : 7
    Enter an element : 8
    Enter an element : 0
    Enter an element : 0
    Enter an element : 2
    Enter an element : 3
    Enter an element : 0
    Enter an element : 0
    Enter an element : 0
    Enter an element : 7
    _____________________
    The Matrix is :
    1 2 3 4
    0 6 7 8
    0 0 2 3
    0 0 0 7
    _____________________
    The matrix is an Upper Triangular Matrix

Also See:   C program for dfs - Depth First Search

 

Check Also

How to find inverse of a matrix in C programming?

In order to find inverse of a matrix in C programming, you need to know ...

Leave a Reply

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