2 D Array In Java

2-D Array: Array is constructed from a number of rows and number of the column it is used to solve real-time problem.

1.Sum of two matrices of the array

import java.util.*;
import java.util.Scanner;
class sum
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("enter the value for second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("sum of given matrix is:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}
OUTPUT:
enter values for first matrix
1
1
1
2
2
2
3
3
3
enter the value for second matrix
1
1
1
2
2
2
3
3
3
sum of the given matrix is:
2 2 2
4 4 4
6 6 6
Translate »