easiest way to get horizontal sum, vertical sum, right diagonal sum & left diagonal sum of given Matrix?
<p>I did the below code to get horizontal sum, vertical sum, right diagonal sum &amp; left diagonal sum of given Matrix &amp; it's working fine, but the below mentioned code can be reduced?. please post your code in C++ or Java. </p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

    int main() {
        int n = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0;
        cout &lt;&lt; "Enter the Size of array" &lt;&lt; endl;
        cin &gt;&gt; n;

        int in[n][n],h_sum[n],v_sum[n];
        cout &lt;&lt; "Enter the array elements" &lt;&lt; endl;

        for(int u = 0; u &lt; n; u++) {
            h_sum[u]=0;
            v_sum[u]=0;

            for(int u1 = 0; u1 &lt;n ; u1++)
                cin&gt;&gt;in[u][u1];
            }
        for(int u = 0; u &lt; n; u++) {
            for(int u1 = 0; u1 &lt; n; u1++)
                h_sum[u] += in[u][u1];

            for(int u2 = 0; u2 &lt; n; u2++)
                v_sum[u] += in[u2][u];

            for(int u3 = 0; u3 &lt; n; u3++) {
                if(u == u3)
                    n1 += in[u][u3];}

            for(int u4 = 0; u4 &lt; n; u4++) {
                if(u + u4 == n - 1)
                    n2+=in[u][u4];
            }
        }
        for(int u = 0;u &lt; n; u++) {
            for(int u1 = 0;u1 &lt; n; u1++)
                cout &lt;&lt; "\t" &lt;&lt; in[u][u1];

            cout &lt;&lt; "\t---&gt;" &lt;&lt; h_sum[u];
            cout &lt;&lt; endl &lt;&lt; endl;
        }

        cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "\t---------------------------------------" &lt;&lt; endl;
        for(int u = 0; u &lt; n; u++) {
            cout &lt;&lt; "\t" &lt;&lt; v_sum[u];
            n3 += h_sum[u];
            n4 += v_sum[u];
        }

        cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
        cout &lt;&lt; "Horizontal sum--&gt;" &lt;&lt; n3 &lt;&lt;endl;
        cout &lt;&lt; "Vertical sum--&gt;" &lt;&lt; n4 &lt;&lt;endl;
        cout &lt;&lt; "Right Dioganal sum--&gt;" &lt;&lt; n1 &lt;&lt;endl;
        cout &lt;&lt; "Left Dioganal sum--&gt;" &lt;&lt; n2 &lt;&lt;endl;

        return 0;
    }
</code></pre>
