C Program to Display Upper Triangular Matrix

«
»

This is a program to display upper triangular matrix.

Problem Description

This C Program displays the upper triangular matrix.

Problem Solution

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).

Program/Source Code

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.

  1.     /*
  2.      * C Program to Display Upper Triangular Matrix
  3.      */
  4.  
  5.     #include <stdio.h>
  6.     void main()
  7.     {
  8.  
  9.         int i, j, r, c, array[10][10];
  10.         printf("Enter the r and c value:");
  11.  
  12.         scanf("%d%d", &r, &c);
  13.         for (i = 1; i <= r; i++)
  14.         {
  15.             for (j = 1; j <= c; j++)
  16.             {
  17.                 printf("array[%d][%d] = ", i, j);
  18.                 scanf("%d", &array[i][j]);
  19.             }
  20.         }
  21.  
  22.         printf("matrix is");
  23.         for (i = 1; i <= r; i++)
  24.         {
  25.             for (j = 1; j <= c; j++)
  26.             {
  27.                 printf("%d", array[i][j]);
  28.             }
  29.             printf("\n");
  30.         }
  31.  
  32.         for (i = 1; i <= r; i++)
  33.         {
  34.             printf("\n");
  35.             for (j = 1; j <= c; j++)
  36.             {
  37.                 if (i >= j)
  38.                 {
  39.                     printf("%d", array[i][j]);
  40.                 }
  41.                 else
  42.                 {
  43.                     printf("\t");
  44.                 }
  45.             }
  46.  
  47.         }
  48.  
  49.         printf("\n\n");
  50.         for (i = 1; i <= r; i++)
  51.         {
  52.             printf("\n");
  53.             for (j = 1; j <= c; j++)
  54.             {
  55. 	            if (j >= i)
  56.                     {
  57.                 	printf("%d", array[i][j]);
  58.             	    }
  59.             	    else 
  60.                     {
  61.                 	//printf("\t");
  62. 	            }
  63.             // printf("\n");
  64.  
  65.         }
  66.  
  67.     }
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
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.

Participate in C Programming Certification Contest of the Month Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.