Monday , December 18 2017
Home / C / Program to Display Upper Triangular Matrix

Program to Display 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

 

In matrix U, all the element below the main diagonal is zero then we say this matrix as an upper triangular matrix.

 

  •  import java.util.*;
        public class UpperTriangularMatrix 
        {
            public static void main(String args[])
            {
                int arr[][] = new int[10][10];
                System.out.println("Enter the order of Matrix: ");   //enter order of matrix
                System.out.println("Enter the rows:");      //enter number of row
                Scanner sy = new Scanner(System.in);
                int a = sy.nextInt();
                System.out.println("Enter the columns:");	//enter number of columns
                Scanner s = new Scanner(System.in);
                int b = s.nextInt();
                if(a == b)
                {        
                     System.out.println("Enter your elements:");	//enter the element of matrix
                      for(int i = 0; i < a; i++)
                      {
                         for(int j = 0; j < a; j++)
                         {      
                             Scanner ss = new Scanner(System.in);
                             arr[i][j] = ss.nextInt();
                             System.out.print(" ");
                         }
                      }
                      System.out.println("You have entered:");
                      for(int i = 0; i < a; i++)
                      {
                          for(int j = 0; j < a; j++)
                          { 
                              System.out.print(arr[i][j] + " ");
                          }
                          System.out.println("");
                      }
                      System.out.println("The Upper Triangular Matrix is:");	
                      for(int i = 0; i < a; i++)	//print upper triangular matrix
                      {
                          for(int j = 0; j < a; j++)
                          {
                              if(i <= j)
                              {
                                System.out.print(arr[i][j] +" ");
                              }
                              else
                              {
                                  System.out.print("0"+" ");
                              }
                           }
                           System.out.println("");
                         }
                      }
                      else
                      {
                          System.out.println("you have entered improper order");
                      }
                 }
            }
        }

     Output:

    Output 1:

    Enter the order of your Matrix
    Enter the rows:
    4
    Enter the columns:
    4
    Enter your elements:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    1
    2
    3
    4
    5
    6
    You have entered:
    1 2 3 4
    5 6 7 8
    9 0 1 2
    3 4 5 6
    The Upper Triangular Matrix is:
    1 2 3 4
    0 6 7 8
    0 0 1 2
    0 0 0 6

     

     

    Output 2:

    Enter the order of your Matrix
    Enter the rows:
    3
    Enter the columns:
    4
    
    you have entered improper order

     

  • #include<stdio.h>
    #include<conio.h>
    void main()
    {
         int a,b,i,j,arr[10][10];
         clrscr();
         printf(“Enter the order of a square matrix: “);   //get the order of matrix
         scanf(“%d%d”,&a,&b);
         if(a==b)           //enter elements in matrix
         {
              printf(“Enter %d elements in the matrix:n”,a*b);
              for(i=0;i<a;i++)
              {
                   for(j=0;j<b;j++)
                   scanf(“%d”,&arr[i][j]);
              }
              printf(“You have entered the following matrix:n”);   //print the entered matrix
              for(i=0;i<a;i++)
              {
                   for(j=0;j<b;j++)
                   printf(“%4d”,arr[i][j]);
                   printf(“n”);
              }
              printf(“Upper triangular matrix for the given matrix is:n”); 
              for(i=0;i<a;i++)	//procedure of find upper triangular matrix
              {
                   for(j=0;j<b;j++)
                   {
                        if(i>j)
                        arr[i][j]=0;
                        printf(“%4d”,arr[i][j]);
                   }
                   printf(“n”);
              }
         }
         else
         printf(“Matrix should be a square matrix,So enter a valid order”);
         getch();
    }

    Output:

    Output 1:

    Enter the order of a square matrix:4
    4
    Enter 16 elements in the matrix:1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    1
    2
    3
    4
    5
    6
    You have entered the following matrix:
    1    2    3    4
    5    6    7    8
    9    0    1    2
    3    4    5    6
    Upper triangular matrix for the given matrix is:
    1    2    3    4
    0    6    7    8
    0    0    1    2
    0    0    0    6

     

    Output 2:

    Enter the order of a square matrix:4
    3
    Matrix should be a square matrix,So enter a valid order.

     

Also See:   c program to print first 20 fibonacci numbers

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

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