pTutorial

New way to learn Programming

Java array exercises with solutions

1 2 3
4 5 6
7 8 9

Java program to print sum of upper triangular matrix

class TringleUpperHalfSum{
    public static void main(String... z)
    {
        sum(new int[][] {{1,2,3},{4,5,6},{7,8,9}});
    }
    static void sum(int d[][])
    {
        int sum=0;
        for(int i=0;i<d.length;i++)
        {
            for(int j=(i+1)-1;j<d[i].length;j++)
            {
                sum=sum+d[i][j];
            }
        }
        System.out.println("sum="+sum);
    }
}


Output

sum=26

See Also


QUOTES OF THE DAY

Beauty's tears are lovelies than her smiles.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER