Code Scrappers


Sum Of Upper Triangular Elements Of a Matrix - C Program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
                 int i, j, r, c, sum = 0;

                 int m[8][8];

                 printf("\n enter the number of rows and columns : ");

                 scanf("%d%d", &r, &c);

                 printf("\nenter the elements\n");

                 for (i = 0; i < r; i++)
                 {
                    for (j = 0; j < c; j++)
                    {
                        scanf("%d", &m[i][j]);
                    }
                 }

                 for (i = 0; i < r; i++)
                 {
                    for (j = 0; j < c; j++)
                    {
                       if (i < j)
                       {
                            sum = sum + m[i][j];
                       }
                    }
                }
                 printf("\n addition of upper triangle elements is : %d", sum);

              }



          



Output

      enter the number of rows and columns : 3
      3

      enter the elements
      1
      2
      3
      4
      5
      6
      7
      8
      9

      addition of upper triangle elements is : 11