w3resource

C Exercises: Display the upper triangular of a given matrix

C Array: Exercise-27 with Solution

Write a program in C to print or display upper triangular matrix.

Pictorial Presentation:

C Exercises:  Display the upper triangular of a given matrix

Sample Solution:

C Code:

#include <stdio.h>

void main()
 {
  int arr1[10][10],i,j,n;
  float determinant=0;
  
  
 printf("\n\nDisplay the upper triangular of a given matrix :\n");
       printf("----------------------------------------------\n");

     printf("Input the size of the square matrix : ");
     scanf("%d", &n); 
	 printf("Input elements in the first matrix :\n");
       for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
            }
        }  
	 printf("The matrix is :\n");
	 for(i=0;i<n;i++)
	 {
	   for(j=0;j<n ;j++)
	     printf("% 4d",arr1[i][j]);
	    printf("\n");
	 }

   printf("\nSetting zero in upper triangular matrix\n");
   for(i=0;i<n;i++)
     {
      printf("\n");
      for(j=0;j<n;j++)
           if(i>=j)
             printf("% 4d",arr1[i][j]);
           else
             printf("% 4d",0);
  }
       printf("\n\n");
}

Sample Output:

Display the upper triangular of a given matrix :                                                              
----------------------------------------------                                                                
Input the size of the square matrix : 3                                                                       
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [0],[2] : 3                                                                                         
element - [1],[0] : 4                                                                                         
element - [1],[1] : 5                                                                                         
element - [1],[2] : 6                                                                                         
element - [2],[0] : 7                                                                                         
element - [2],[1] : 8                                                                                         
element - [2],[2] : 9                                                                                         
The matrix is :                                                                                               
   1   2   3                                                                                                  
   4   5   6                                                                                                  
   7   8   9  
Setting zero in upper triangular matrix                                                                       
                                                                                                              
   1   0   0                                                                                                  
   4   5   0                                                                                                  
   7   8   9   

Flowchart:

Flowchart: Display the upper triangular of a given matrix

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to print or display the lower triangular of a given matrix.
Next: Write a program in C to calculate determinant of a 3 x 3 matrix.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts