wap to subtract digonals elements of a matrix in cpp hackerrank program

subtract of digonals elements of a matrix in cpp hackerrank program:

solution:


#include<iostream>
using namespace std;
int main()
{
    int n,i,j,k,l,arr[100][100],sum=0,sum1=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>arr[i][j];
        }
    }
     for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
         
          if(i==j)
          {
              sum+=arr[i][j];
             
          }
           if(i+j==n-1)
          {
            sum1+=arr[i][j];
          }
        
         
       
        }
    }
  
   if(sum1>sum)
   {
       cout<<sum1-sum;
   }
   else
   {
       cout<<sum-sum1;   }

}


Output:

(5+34+4) - (9+34+9)=9;



Output:



0 comments: