C program to calculate sum of Upper Diagonal Elements of matrix

C program to calculate sum of Upper Diagonal Elements of matrix:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
#include<conio.h>

main()
{
   int i,j,a[10][10],sum,rows,columns;

   printf("Enter the number of Rows :\n");
   scanf("%d",&rows);

   printf("Enter the number of Columns :\n");
   scanf("%d",&columns);

   //Accepting Elements in Matrix
   printf("\nEnter %d*%d Matrix:\n",rows,columns);
   for(i=0;i<rows;i++)
      for(j=0;j<columns;j++)
        {
         scanf("%d", &a[i][j]);
        }

   //Addition of Diagonal Elements
   sum = 0;
   for (i = 0; i < rows; i++)
      for (j = 0; j < columns; j++)
        {
          // Condition for Upper Diagonal
          if (i < j)
          {
            sum = sum + a[i][j];
          }
        }

   //Printing Result
   printf("\nSum of Upper Triangle Elements :%d",sum);
}

OUTPUT:

Enter the number of Rows :
3
Enter the number of Columns :
3

Enter 3*3 Matrix:
4 5 7
1 3 8
9 8 7

Sum of Upper Triangle Elements :20

 

Leave a Reply

Your email address will not be published. Required fields are marked *

seventeen − 1 =