Categories
Interview java matrix

multiply two matrix using java with example

In this post, we will learn about multiply two matrix in java with example

We are declaring two matrix named as a,b and one more variable mul to store the multiplication of a&b matrix

Example
public class MatrixMultiplication {

	public static void main(String[] args) {
		try {

			int a[][] = { { 50, 60 }, { 20, 15 } };
			int b[][] = { { 25, 20 }, { 10, 5 } };

			// Declaring the diff matrix
			int[][] mul = new int[a.length][b.length];

			// multiply 2 matrices
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < b.length; j++) {
					mul[i][j] = 0;
					for (int k = 0; k < 2; k++) {
						mul[i][j] += a[i][k] * b[k][j];
					}
					System.out.print(mul[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
1850  1300 
650   475 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/MatrixMultiplication.java

Related Articles

subtracting two matrix in java with example

Categories
Interview java matrix

subtracting two matrix in java with example

In this post, we will learn about subtracting two matrix in java with example

We are declaring two matrix named as a,b and one more variable diff to store the subtraction of a&b matrix

Example
public class SubtractingMatrix {

	public static void main(String[] args) {
		try {

			int a[][] = { { 50, 60 }, { 20, 15 } };
			int b[][] = { { 25, 20 }, { 10, 5 } };

			// Declaring the diff matrix
			int[][] diff = new int[a.length][b.length];

			// Subtracting two matrix
			for (int i = 0; i < a.length; i++) {

				for (int j = 0; j < b.length; j++) {
					diff[i][j] = a[i][j] - b[i][j];
				}
			}

			// Printing the diff matrix
			for (int i = 0; i < diff.length; i++) {
				for (int j = 0; j < diff[i].length; j++) {
					System.out.print(diff[i][j] + " ");
				}
				System.out.println();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
Output
25 40 
10 10 
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/SubtractingMatrix.java

Related Articles

adding two matrix in java with example

Categories
Interview java

Print String reverse using java with example

Print String reverse in java

Print String in reverse using java with example. This is most asked qustion in java interviews In below example we will see that.

Example

public class StringReverseExample {

  public static void main(String[] args) {
    String s = "java";
    int stringLength = s.length();
    // In Below for loop starting will be length-1
    // The end will 0 the index
    for (int i = stringLength - 1; i >= 0; i--) {
      // we used charAt method to get value from the string
      // instead of println we used print to print on same line
      System.out.print(s.charAt(i));
    }
  }
}

Output

avaj

String reverse without for loop

public class StringReverse {

	public static void main(String[] args) {
		String s = "Java";
		// Below we used StringBuilder to print string in reverse
		StringBuilder builder = new StringBuilder(s);
		System.out.println(builder.reverse());
	}

}

Output

avaJ

To know more about StringBuilder, please refer below link
https://beginnersbug.com/stringbuilder-java/

Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/StringReverseExample.java