Program in Java: Sum of each row and each column in 3x3 matrix.
import java.util.Scanner;
class twodim
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int A[][] = new int[3][3];
//3 rows and 3 columns
//outer loop is for rows
//inner loop is for columns
System.out.println("Enter values into the matrix:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("The values of the matrix are:");
for(int i=0;i<3;i++)
{
int rs=0,cs=0; //row sum and column sum
for(int j=0;j<3;j++)
{
rs=rs+A[i][j];
cs=cs+A[j][i];
}
System.out.println("Sum of the "+i+"th row is"+rs);
System.out.println("Sum of the "+i+"th column is"+cs);
}
}
}
class twodim
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int A[][] = new int[3][3];
//3 rows and 3 columns
//outer loop is for rows
//inner loop is for columns
System.out.println("Enter values into the matrix:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("The values of the matrix are:");
for(int i=0;i<3;i++)
{
int rs=0,cs=0; //row sum and column sum
for(int j=0;j<3;j++)
{
rs=rs+A[i][j];
cs=cs+A[j][i];
}
System.out.println("Sum of the "+i+"th row is"+rs);
System.out.println("Sum of the "+i+"th column is"+cs);
}
}
}