Monday 9 June 2014

Sum of elements of two 2D arrays having 3 rows and 3 columns

import javax.swing.JOptionPane;
public class sumofmatrix
 {
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;
System.out.print("enter elements of matrix A");
for( i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
a[i][j]=Integer.parseInt(JOptionPane.showInputDialog(" "));
}

}
System.out.print("enter elements of matrix B");
for( i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
b[i][j]=Integer.parseInt(JOptionPane.showInputDialog(" "));
}

}
System.out.print("\nA= ");
for( i=0; i<=2; i++)
{
System.out.print("\n");

for(j=0; j<=2; j++)
{
System.out.print(a[i][j]);
}
}
System.out.print("\nB= ");
for( i=0; i<=2; i++)
{
System.out.print("\n");

for(j=0; j<=2; j++)
{
System.out.print(b[i][j]);
}
}
for( i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
c[i][j]=a[i][j]+b[i][j];
}

}
System.out.print("\nsum= ");
for( i=0; i<=2; i++)
{
System.out.print("\n");

for(j=0; j<=2; j++)
{
System.out.print(c[i][j]);
}
}
}
}
output:
enter matrix A
1
2
3
4
5
6
7
8
9
enter matrix B
1
2
3
4
5
6
7
8
9

A= 123
      456
      789
B= 123
      456
      789
sum=  2 4 6
         8 10 12
         14 16 18

No comments:

Post a Comment

SAY HELLO!!