Monday, 2 October 2017

Write a C program to find the sum of elements 

below and above diagonal of a matrix.


Program Code:

#include<stdio.h>

#include<conio.h>

void main()
     
{

   int a[4][4];

   int i, j, x=0, y=0;
       
   clrscr();

   printf("Enter number of array elements into a[4][4]\n");

   for(i=0;i<=3;i++)

   {

     for(j=0;j<=3;j++)

    {

      scanf("%d",&a[i][j]);

     }

  }




for(i=0;i<=3;i++)

{

  for(j=0;j<=3;j++)

  {

   printf("%d ",a[i][j]);

  }

}

printf("\n");

 for(i=0;i<=3;i++)

 {

   for(j=0;j<=3;j++)

   {

     if(j>i)

       {  

       x = x + a[i][j];

      }

     else if(j<i)

      {

      y = y + a[i][j];

      }

      }

  printf("Sum of elements above diagonal=%d\n",x);

  printf("Sum of elements below diagonal=%d\n",y);

  getch();

  }



Thanks

Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput