C program to calculate sum of Upper Triangular Elements(2D-Array)

C program to calculate sum of Upper Triangular Elements(2D-Array)

#include<stdio.h>

int main()
{

   int i, j, a[10][10], sum, rows, columns;

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

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


   for (i = 0; i < rows; i++)
   {
      for (j = 0; j < columns; j++)
 {
         printf("\nEnter the Element a[%d][%d] : ", i, j);
         scanf("%d", &a[i][j]);
      }
   }
   //Addition of all Diagonal Elements
   sum = 0;
   for (i = 0; i < rows; i++)
   {
      for (j = 0; j < columns; j++)
 {
         // Condition for Upper Triangle
         if (i < j)
        {
            sum = sum + a[i][j];
         }
      }
   }

   printf("sum of Upper Triangle Elements : %d\n", sum);   //Print out the Result

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics