Print upper triangular matrix using Array in C


This program is a C program that checks whether a given matrix is an upper triangular matrix or not. The program begins by asking the user to enter the number of rows and columns of the matrix. It then takes input from the user for the matrix values using a nested for loop. The program then prints the matrix entered by the user.

The program then uses another nested for loop to check if the given matrix is an upper triangular matrix or not. The loop checks if the current element is in the upper triangle of the matrix (i.e i > j) and if the value of that element is 0. If the element is in the upper triangle and its value is 0, the variable 'z' is incremented. If it is not in the upper triangle, the variable 'o' is incremented.

Finally, the program checks if the total number of elements in the matrix (r*c) is equal to the number of elements in the upper triangle + number of elements not in the upper triangle. If it is, the program prints that the given matrix is an upper triangular matrix, otherwise, it prints that it is not an upper triangular matrix.

Source Code

#include<stdio.h>
int main()
{
  int a[10][10],i,j,r,x,c,z=0,o=0;
  printf("\nEnter the No of Rows:");
  scanf("%d",&r);
  printf("\nEnter the No of Columns:");
  scanf("%d",&c);
  printf("\nEnter the matrix Values:");
  for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
          scanf("%d",&a[i][j]);
      }
  }
  for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
          printf("\t%d",a[i][j]);
      }
      printf("\n");
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      if(i>j)
      {
        if(a[i][j]==0)
        {
           z++;
        }
      }
      else
      {
        o++;
      }
    }
  }
  if((r*c)==(x+o))
  {
    printf(" %d*%d Upper Matrix",r,c);
  }
  else
  {
    printf(" %d*%d Not Upper Matrix",r,c);
  }
  return 0;
}
To download raw file Click Here

Output

Enter the No of Rows:3
Enter the No of Columns:3
Enter the matrix Values:1
1
1
0
1
1
0
0
1
   1  1  1
   0  1  1
   0  0  1
   
3*3 Upper Matrix


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C