Matrix Addition program using java
package day1;
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
int a[][] = new int[2][2];
int b[][] = new int[2][2];
int sum[][] = new int[2][2];
Scanner sc = new Scanner(System.in);
System.out.println("enter elements into a");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("enter elements into b");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
b[i][j] = sc.nextInt();
}
}
System.out.println("sum is");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
OutPut:
enter elements into a
1
2
3
4
enter elements into b
5
6
7
8
sum is
6 8
10 12
package day1;
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
int a[][] = new int[2][2];
int b[][] = new int[2][2];
int sum[][] = new int[2][2];
Scanner sc = new Scanner(System.in);
System.out.println("enter elements into a");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("enter elements into b");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
b[i][j] = sc.nextInt();
}
}
System.out.println("sum is");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
OutPut:
enter elements into a
1
2
3
4
enter elements into b
5
6
7
8
sum is
6 8
10 12
0 comments:
Post a Comment