Upper triangular matrix in c






C code to print or display upper triangular matrix


#include<stdio.h>
int main(){
  int a[3][3],i,j;
  float determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

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


   return 0;
}

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
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




Alogrithm:
**







3 comments:

Java Examples said...

your are printing lower triangular matrix and it is not full program....this is directly put 0 in matrix

Anonymous said...

sir
ther is an some problem because upper and lower triangular are both use the diagonal elements.
so thecondition should be if(i<=j && i==j) for lower triangular and if(i>=j && i==j) for upper triangular so that the diagonal elements must be shown....
charnjit,,b.c.a. 2nd year.

Unknown said...

U R PRINTING LOWER TRIANGULAR MATRIX , GUYS THIS IS BIT WRONG PROGRAM,THERE R SOME FIXES ... ,ITS LOWER TRIANGULAR!