14. Write a java Program to Add Two Matrices?
import java.util.Scanner;
public class AddMatrix {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter rows");
int rows=sc.nextInt();
System.out.println("enter cols");
int cols=sc.nextInt();
int [][]a=new int[rows][cols];
int [][]b=new int[rows][cols];
System.out.println("enter First matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("enter Second matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
b[i][j]=sc.nextInt();
}
}
System.out.println("After addition");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
System.out.print(a[i][j]+b[i][j]);
}
System.out.println();
}
}
}
import java.util.Scanner;
public class AddMatrix {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter rows");
int rows=sc.nextInt();
System.out.println("enter cols");
int cols=sc.nextInt();
int [][]a=new int[rows][cols];
int [][]b=new int[rows][cols];
System.out.println("enter First matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("enter Second matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
b[i][j]=sc.nextInt();
}
}
System.out.println("After addition");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
System.out.print(a[i][j]+b[i][j]);
}
System.out.println();
}
}
}
No comments:
Post a Comment