Monday, 30 January 2017

Program to check Upper Triangular matrix

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

                                                             upper-triangular-matrix-formula.gif (130×42)
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.gif (147×76)

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=;i<n;i++)           // get Input in the matrix
        {
            for(int j=;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=;i<n;i++)
        {
            for(int j=;j<n;j++)
            {
                System.out.print(Arr[i][j]+"t");
            }
            System.out.println();
        }
        System.out.println("_____________________");
        
        int k=;
        
        for(int i=;i<n;i++)                         // Check the matrix is Upper Triangular Matrix or not
        {
            for(int j=;j<i;j++)
            {
                if(Arr[i][j]!=) // All elements below the diagonal must be zero
                {
                    p=1;
                    break;
                }
            }
        }
        
        if(p==)
            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

No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...