Posts

Showing posts with the label program to perform addition and substraction of two matrices.

write a java program to perform addition and substraction of two matrices.

  write a java program to perform addition and substraction  of two matrices. Program : import java.util.*; public class matrixopera { public static void main(String args[]) { Scanner sc=new Scanner( System.in ); System.out.println("Enter the order of the matrix :"); int r=sc.nextInt(); int c=sc.nextInt(); int i, j, k; int a[ ][ ]=new int[r][c]; int b[ ][ ]=new int[r][c]; int res[ ][ ]=new int[r][c]; System.out.println("Enter the elements for matrixA:"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { a[i][j]=sc.nextInt(); } } System.out.println("Enter the elements for matrix B:"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { b[i][j]=sc.nextInt(); } } //matrix addition for(i=0;i<r;i++) { for(j=0;j<c;j++) { res[i][j]=a[i][j]+b[i][j]; } } System.out.println("Addition of  matrix :"); System.out.println("Resultant matrix is:"); for(i=0;i<r;i++) { System.out.println(); for(j=0;j<c;j++) { System...