instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
add two matrices
Posted: Jan 24, 2015 8:41 AM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: add two matrices
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
Advertisement
package com.instanceofjavaforus; import java.util.Scanner; class Add2Matrix { public static void main(String args[]) { int rows, cols, c, d; Scanner in = new Scanner(System.in); System.out.println("Please Enter number of rows and columns"); rows = in.nextInt(); cols = in.nextInt(); int first[][] = new int[rows][cols]; int second[][] = new int[rows][cols]; int sum[][] = new int[rows][cols]; System.out.println("Please Enter elements of first matrix"); for ( c = 0 ; c < rows ; c++ ) for ( d = 0 ; d < cols ; d++ ) first[c][d] = in.nextInt(); System.out.println("Please Enter elements of second matrix"); for ( c = 0 ; c < rows ; c++ ) for ( d = 0 ; d < cols ; d++ ) second[c][d] = in.nextInt(); for ( c = 0 ; c < rows ; c++ ) for ( d = 0 ; d < cols ; d++ ) sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices System.out.println("Sum of entered matrices:-"); for ( c = 0 ; c < rows ; c++ ) { for ( d = 0 ; d < cols ; d++ ) System.out.print(sum[c][d]+"\t"); System.out.println(); } } } Output: Please Enter number of rows and columns 3 3 Please Enter elements of first matrix 1 1 1 1 1 1 1 1 1 Please Enter elements of second matrix 2 2 2 2 2 2 2 2 2 Sum of entered matrices:- 3 3 3 3 3 3 3 3 3
Read: add two matrices