Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MatrixAddSub{
- static int r, c, A[][], B[][], C[][];
- static Scanner sc = new Scanner(System.in);
- static void input(){
- int i, j;
- System.out.print("\nRows: ");
- r = sc.nextInt();
- System.out.print("\nColoumns: ");
- c = sc.nextInt();
- A = new int[r][c];
- B = new int[r][c];
- C = new int[r][c];
- System.out.println("Enter the elements of A:-");
- for(i = 0; i < r; i++)
- for(j = 0; j < c; j++)
- A[i][j] = sc.nextInt();
- System.out.println("Enter the elements of B:-");
- for(i = 0; i < r; i++)
- for(j = 0; j < c; j++)
- B[i][j] = sc.nextInt();
- }
- static void negate(){
- int i, j;
- for(i = 0; i < r; i++)
- for(j = 0; j < c; j++)
- B[i][j] = -B[i][j];
- }
- static void add(){
- int i, j;
- for(i = 0; i < r; i++)
- for(j = 0; j < c; j++)
- C[i][j] = A[i][j] + B[i][j];
- }
- static void output(){
- int i, j;
- System.out.println("Matrix A is :-");
- for(i = 0; i < r; i++){
- for(j = 0; j < c; j++)
- System.out.print(A[i][j]+" ");
- System.out.println();
- }
- System.out.println("Matrix B is :-");
- for(i = 0; i < r; i++){
- for(j = 0; j < c; j++)
- System.out.print(B[i][j]+" ");
- System.out.println();
- }
- System.out.println("The result is:-");
- for(i = 0; i < r; i++){
- for(j = 0; j < c; j++)
- System.out.print(C[i][j]+" ");
- System.out.println();
- }
- }
- public static void main(String[] args){
- input();
- System.out.print("1.Add 2.Subtract: ");
- if(sc.nextInt() == 2)negate();
- add();
- output();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement