Tuesday, May 17, 2011

Summation of elements of upper triangle of matrix

Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBsum {

static void triangleBsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i>=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle B is:"+c);
}
public static void main(String[] args)
{
triangleBsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}

}

No comments: