Thursday, 16 June 2011

Program to Print the sum of Rows of 2-D Array

class Sum
{
public static void main ( String args[ ])
{
int s[ ] = new int[3];
int a[ ][ ] = {{1,2,3},{3,4,5},{6,7,8}};
int  r, c;

for(r=0 ; r<a.length ; r++)
{
System.out.print( "|" );
for(c=0 ; c<a.length ; c++)
{
System.out.print( a[r][c]+" "); 
}
System.out.println( "|" );
}

for(r=0 ; r<a.length ; r++)
{
s[r] = 0;
}
for(r=0 ; r<a.length ; r++)
{
for(c=0 ; c<a.length ; c++)
{
s[r] = s[r] + a[r][c] ;
}
}
for(r=0 ; r<3 ; r++)
{
System.out.printlln("The sum of "+(r+1)+" row of an array is :" +s[r]);
}
}
}

OUTPUT 


















No comments:

Post a Comment