/* program to illustrate the concept of addition of two user entered matrix */
/* c[i][j] = a[i][j] + b[i][j] */
import java.util.*;
class ArrayAdditon
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [][]ar1=new int[2][3];
int [][]ar2=new int[2][3];
int [][]ar3=new int[2][3];
int i,j;
//////first matrix
System.out.println("Enter the elements of the matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar1[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix First are :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
///////////// 2nd matrix
System.out.println("Enter the elements of the second matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar2[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix second are :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar2[i][j]+" ");
}
System.out.println();
}
///// Additon
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar3[i][j]=ar1[i][j]+ar2[i][j];
}
}
/// display
System.out.println("Additon of the matrix is : ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar3[i][j]+" ");
}
System.out.println();
}
}
}
Output
Enter the elements of the matrix :
2
3
4
5
4
5
the elements of the matrix First are :
2 3 4
5 4 5
Enter the elements of the second matrix :
1
2
3
4
5
6
the elements of the matrix second are :
1 2 3
4 5 6
Additon of the matrix is :
3 5 7
9 9 11
/* c[i][j] = a[i][j] + b[i][j] */
import java.util.*;
class ArrayAdditon
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [][]ar1=new int[2][3];
int [][]ar2=new int[2][3];
int [][]ar3=new int[2][3];
int i,j;
//////first matrix
System.out.println("Enter the elements of the matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar1[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix First are :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
///////////// 2nd matrix
System.out.println("Enter the elements of the second matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar2[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix second are :");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar2[i][j]+" ");
}
System.out.println();
}
///// Additon
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
ar3[i][j]=ar1[i][j]+ar2[i][j];
}
}
/// display
System.out.println("Additon of the matrix is : ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(ar3[i][j]+" ");
}
System.out.println();
}
}
}
Output
Enter the elements of the matrix :
2
3
4
5
4
5
the elements of the matrix First are :
2 3 4
5 4 5
Enter the elements of the second matrix :
1
2
3
4
5
6
the elements of the matrix second are :
1 2 3
4 5 6
Additon of the matrix is :
3 5 7
9 9 11
No comments:
Post a Comment