Java Program for Matrix Multiplication and Transpose using Input/Output Stream.

import java.io.*;
class Mat
{
  public static void main (String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
      System.out.println ("1. Transpose of Matrix");
      System.out.println ("2. Matrix multiplication");
      System.out.println ("Any other choice for exit the prg");
      System.out.print ("\n\nEnter Your choice");
    int ch = Integer.parseInt (br.readLine ());
    switch (ch)



      {
      case 1:
    System.out.println ("Enter the order of matrix");
    int n, m;
      n = Integer.parseInt (br.readLine ());
      m = Integer.parseInt (br.readLine ());
    int a[][] = new int[10][10];
      System.out.print ("enter the " m * n " elements of matrix");
    for (int i = 0; i < n; i++)
      {
        for (int j = 0; j < m; j++)
          {
        a[i][j] = Integer.parseInt (br.readLine ());
      }} System.out.println ("Origional Matrix is::");
    show (n, m, a);
    System.out.println ("ABer Transpose Matrix become::");
    for (int i = 0; i < m; i++)
      {
        for (int j = 0; j < n; j++)



          {
        System.out.print (a[j][i] " ");
          } System.out.println ();
      } break;
      case 2:
    int i, j, s;
    int m1, n1, p, q;
    int a1[][] = new int[10][10];
    int b[][] = new int[10][10];
    int c[][] = new int[10][10];
    System.out.print ("Enter no. of Row of First Matrix->");
    m1 = Integer.parseInt (br.readLine ());
    System.out.print ("Enter no. of Column of First Matrix->");
    n1 = Integer.parseInt (br.readLine ());
    System.out.print ("Enter no. of Row of Second Matrix->");
    p = Integer.parseInt (br.readLine ());
    System.out.print ("Enter no. of Column of Second Matrix->");



    q = Integer.parseInt (br.readLine ());
    if (n1 != p)
      {
        System.out.
          println
          ("\n\nMatrix Multyply Fail!!---Matrix should be Identical");
        break;
      }
    System.out.println ("Enter First Matrix");
    for (i = 0; i < m1; i++)
      {
        for (j = 0; j < n1; j++)
          {
        System.out.print ("Enter Element at " (i 1)
                  " Row and " (j 1) " Column ->");
        a1[i][j] = Integer.parseInt (br.readLine ());
          }
      }
    System.out.println ("Enter Second Matrix");
    for (i = 0; i < p; i++)
      {
        for (j = 0; j < q; j++)


          {
        System.out.print ("Enter Element at " (i 1)
                  " Row and " (j 1) " Column ->");
        b[i][j] = Integer.parseInt (br.readLine ());
          }
      }
    for (i = 0; i < m1; i++)
      {
        for (j = 0; j < q; j++)
          {
        c[i][j] = 0;
        for (s = 0; s < n1; s++)
          c[i][j] += a1[i][s] * b[s][j];
          }
      }
    System.out.println ("First Array:");
    show (m1, n1, a1);
    System.out.println ("Second Array:");
    show (p, q, b);
    System.out.println ("Multiplied Array:");


    show (m1, q, c);
    break;
      default:
      }
  }
  public static void show (int n, int m, int a[][])
  {
    for (int i = 0; i < n; i++)
      {
    for (int j = 0; j < m; j++)
      {
        System.out.print (a[i][j] " ");
      } System.out.println ();
}}}
 
Java Program for Matrix Multiplication and Transpose using Input/Output Stream. Java Program for Matrix Multiplication and Transpose using Input/Output Stream. Reviewed by on September 06, 2019 Rating: 5

No comments:

Powered by Blogger.