Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication17;
- import java.util.*;
- import java.lang.*;
- import java.math.*;
- public class JavaApplication17 {
- public static int[][] addMatricies(int[][] m1, int[][] m2)
- {
- int[][] result = new int[m1.length][m1[0].length];
- for (int row=0; row<m1.length; row++) {
- for (int col=0; col<m1[row].length; col++) {
- result[row][col] = m1[row][col] + m2[row][col];
- }
- }
- return result;
- }
- public static int[][] minusMatricies(int[][] m1, int[][] m2)
- {
- int[][] result = new int[m1.length][m1[0].length];
- for (int row=0; row<m1.length; row++) {
- for (int col=0; col<m1[row].length; col++) {
- result[row][col] = m1[row][col] - m2[row][col];
- }
- }
- return result;
- }
- public static void sumColumn(int[][] m) {
- int r = m.length;
- int c = m[0].length;
- double total = 0;
- for (int row = 0; row < r; row++) {
- total = 0;
- for (int column = 0; column < c ; column++) {
- total += m[row][column];
- }
- System.out.println("The sum for column "+ row + " is " + total);
- }
- }
- public static void sumRow(int[][] m) {
- int r = m.length;
- int c = m[0].length;
- double total = 0;
- for (int column = 0; column < c ; column++) {
- total = 0;
- for (int row = 0; row < r; row++) {
- total += m[row][column];
- }
- System.out.println("The sum for row "+ column+ " is " + total);
- }
- }
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner scanner = new Scanner(System.in);
- System.out.print(" enter row : ");
- int row = scanner.nextInt();
- System.out.print(" enter column : ");
- int column = scanner.nextInt();
- int[][] a = new int[row][column];
- int[][] b = new int[row][column];
- int[][] c = new int[row][column];
- System.out.println("enter first mattrix: ");
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<column;j++)
- {
- a[i][j]=scanner.nextInt();
- }
- System.out.println("");
- }
- System.out.println("enter second mattrix: ");
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<column;j++)
- {
- b[i][j]=scanner.nextInt();
- }
- System.out.println("");
- }
- System.out.println("Add :");
- c=addMatricies(a,b);
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<column;j++)
- {
- System.out.print(c[i][j]+" ");
- }
- System.out.println("");
- }
- System.out.println("Minus");
- c=minusMatricies(a,b);
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<column;j++)
- {
- System.out.print(c[i][j]+" ");
- }
- System.out.println("");
- }
- System.out.println("Sum of row of A is : ");
- sumRow(a);
- System.out.println("Sum of column of B is : ");
- sumColumn(b);
- }
- }
Add Comment
Please, Sign In to add comment