Contact Learn C
Copy Program Share Program

Program 167: To Print Sum of Upper and Lower Triangle of matrix

Program 167:

#include<stdio.h>
main()
{ 
 int i,j,rows,col,uppersum=0,lowersum=0;
 printf("Enter number of rows\n");
 scanf("%d %d",&rows,&col);
 int a[rows][col];
 if(col==rows)
 {
   //Taking input for matrix
 printf("Enter Matrix 1\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   scanf("%d",&a[i][j]);
  }
  }
  printf("The given Matrix is\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
  }
//Upper Triangle
  for(i=0;i<rows;i++)
  {
    for(j=0;j<col;j++)
    {
     if(i<j)
     {
      uppersum+=a[i][j];
     }
    }
   
  }
  
//Lower Triangle
  for(i=0;i<rows;i++)
  {
    for(j=0;j<col;j++)
    {
     if(i>j)
     {
      lowersum+=a[i][j];
     }
    }
   
  }
    printf("Upper Triangle Sum of Matrix is %d\n",uppersum);
    printf("Lower Triangle Sum of Matrix is %d\n",lowersum);
}
 else
 {
  printf("Not Possible\n");
 }
 
  
}
Explanation:

//Coming Soon...

Output:

Print Sum  of Upper and Lower Triangle of matrix

No comments:

Post a Comment

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide