This is a program to display upper triangular matrix.
This C Program displays the upper triangular matrix.
1. Create a matrix and define its elements.
2. To print the elements at upper triangle, run a nested for loop with two iterators(i and j) but print only those elements which have index i(row index) greater or equal to j(column index).
Here is source code of the C Program to display upper triangular matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Display Upper Triangular Matrix
*/
#include <stdio.h>
void main()
{
int i, j, r, c, array[10][10];
printf("Enter the r and c value:");
scanf("%d%d", &r, &c);
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("array[%d][%d] = ", i, j);
scanf("%d", &array[i][j]);
}
}
printf("matrix is");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("%d", array[i][j]);
}
printf("\n");
}
for (i = 1; i <= r; i++)
{
printf("\n");
for (j = 1; j <= c; j++)
{
if (i >= j)
{
printf("%d", array[i][j]);
}
else
{
printf("\t");
}
}
}
printf("\n\n");
for (i = 1; i <= r; i++)
{
printf("\n");
for (j = 1; j <= c; j++)
{
if (j >= i)
{
printf("%d", array[i][j]);
}
else
{
//printf("\t");
}
// printf("\n");
}
}
1. Declare a matrix of some fixed capacity, take its order as input from users and define the elements of the matrix.
2. Again start a nested for loop with two iterators i and j. i keeping track of row and j keeping track of column.
3. Put a condition inside this loop, if value of i(row) is greater than or equal to j(column), only then the element having index i and j belongs to upper triangle, print it, else continue with the loop.
Enter the r and c value:3 3 array[1][1] = 1 1 1 array[1][2] = array[1][3] = array[2][1] = 1 1 0 array[2][2] = array[2][3] = array[3][1] = 2 0 0 array[3][2] = array[3][3] = matrix is 111 110 200 1 11 200
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C Internship
- Apply for Computer Science Internship
- Check C Books