Example:
If elements of matrix are:
1 2 3
0 5 6
0 0 9
Output: Matrix is upper triangular
Required knowledge:
Basic C programming, For loop, Array, MatrixUpper triangular matrix
Upper triangular matrix is a special type of square matrix whose all elements below the main diagonal is zero.Algorithm to find upper triangular matrix:
To check whether a matrix is upper triangular or not we need to check whether all elements below main diagonal are zero.For any matrix A if all elements Aij = 0 (Where i ≥ j)
If A[i][j] == 0 and i > j then it is upper triangular matrix.
Program:
/**
* C program to find upper triangular matrix
*/
#include <stdio.h>
int main()
{
int A[3][3];
int row, col, isUpper;
/*
* Reads elements in matrix from user
*/
printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
/*
* Checks whether the matrix is Upper triangular
*/
isUpper = 1;
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
/*
* If elements below the main diagonal (col<row)
* is not equal to zero then it is not upper triangular matrix
*/
if(col<row && A[row][col]!=0)
{
isUpper = 0;
}
}
}
/*
* If it is upper triangular matrix
*/
if(isUpper==1)
{
printf("This is a Upper triangular matrix.\n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
if(A[row][col] != 0)
{
printf("%d ", A[row][col]);
}
}
printf("\n");
}
}
else
{
printf("This is Not a Upper triangular matrix.");
}
return 0;
}
Output
Enter elements in matrix of size 3x3:
1 2 3
0 5 6
0 0 9
This is a Upper triangular matrix.
1 2 3
5 6
9
1 2 3
0 5 6
0 0 9
This is a Upper triangular matrix.
1 2 3
5 6
9
Happy coding ;)
You may also like
- Array and Matrix programming exercises index.
- C program to find lower triangular matrix.
- C program to find sum of main diagonal elements of a matrix.
- C program to find sum of opposite diagonal elements of a matrix.
- C program to find sum of each row and columns of a matrix.
- C program to interchange diagonals of a matrix.
- C program to multiply two matrices.
- C program to perform scalar matrix multiplication.
- C program to find factorial of any number.
- C program to find HCF of two numbers.
- C program to find LCM of two numbers.
- C program to find reverse of an array.
- C program to search an element in an array.
- C program to sort elements of array in ascending order.
- C program to sort elements of array in descending order.