public class SumOfEachRowAndColumn {
public static void main(String args[])
{
int a[][] = new int[3][3];
int sumOfRow = 0;
int sumOfColumn = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter element of Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Value of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
//Clculating the sum of rows
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sumOfRow = sumOfRow + a[i][j];
}
System.out.println("Sum of " + (i + 1) + " row is " + sumOfRow);
sumOfRow = 0;
}
//Clculating the sum of column
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sumOfColumn = sumOfColumn + a[j][i];
}
System.out.println("Sum of " + (i + 1) + " column is " + sumOfColumn);
sumOfColumn = 0;
}
}
}
Output:
Enter element of Matrix
1
2
3
4
5
6
1
2
3
Value of Matrix is
1 2 3
4 5 6
1 2 3
Sum of 1 row is 6
Sum of 2 row is 15
Sum of 3 row is 6
Sum of 1 column is 6
Sum of 2 column is 9
Sum of 3 column is 12
BUILD SUCCESSFUL (total time: 7 seconds)
public static void main(String args[])
{
int a[][] = new int[3][3];
int sumOfRow = 0;
int sumOfColumn = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter element of Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Value of Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
//Clculating the sum of rows
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sumOfRow = sumOfRow + a[i][j];
}
System.out.println("Sum of " + (i + 1) + " row is " + sumOfRow);
sumOfRow = 0;
}
//Clculating the sum of column
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
sumOfColumn = sumOfColumn + a[j][i];
}
System.out.println("Sum of " + (i + 1) + " column is " + sumOfColumn);
sumOfColumn = 0;
}
}
}
Output:
Enter element of Matrix
1
2
3
4
5
6
1
2
3
Value of Matrix is
1 2 3
4 5 6
1 2 3
Sum of 1 row is 6
Sum of 2 row is 15
Sum of 3 row is 6
Sum of 1 column is 6
Sum of 2 column is 9
Sum of 3 column is 12
BUILD SUCCESSFUL (total time: 7 seconds)
|
No comments:
Post a Comment