Thursday 12 March 2015

Cpp Program to display Upper Triangular form of a matrix.



This is a Cpp program which display the upper triangular form of a given matrix.

To display the upper triangular form of a given matrix we make all the elements below the diagonal row as zero.





See the figure below,
Upper Triangular form of a given matrix




Download Code
PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int rows,columns,i,j;
    cout << "Enter order of matrix  \t:";
    cin >> rows;
    cin >> columns;
    int matrix[rows][columns];
    cout << "\n\nEnter elements of a matrix\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << "Enter element m" << i+1 << j+1 << ": \t";
            cin >> matrix[i][j];
        }
    }
    cout << "\n\nMATRIX is :\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }
    cout << "\n\nIts Upper Triangular form is: \n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            if(i<=j)
                cout << matrix[i][j] << "\t";
            else
                cout << 0 << "\t";
        }
        cout << endl;
    }
    return 0;
}


OUTPUT : 
Upper Triangular form of a given matrix


Download Code