Sunday, February 19, 2017

Write a JAVA program to determine the addition of two matrices

import java.util.Scanner;

class AddTwoMatrix
{
   public static void main(String args[])
   {
      int c,d;
      Scanner in = new Scanner(System.in);

      int first[][] = new int[3][3];
      int second[][] = new int[3][3];
      int sum[][] = new int[3][3];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
             sum[c][d] = first[c][d] + second[c][d];

      System.out.println("Sum of entered matrices:-");

      for ( c = 0 ; c < 3 ; c++ )
      {
         for ( d = 0 ; d < 3 ; d++ )
            System.out.print(sum[c][d]+"\t");

         System.out.println();
      }
   }
}


output:
>java AddTwoMatrix
Enter the elements of first matrix
1 2 3
4 5 6
7 8 9
Enter the elements of second matrix
7 8 9
4 5 6
1 2 3
Sum of entered matrices:-
8       10      12
8       10      12

8       10      12

No comments:

Post a Comment

Write a JAVA program to sort an array of Strings

import java.util.Scanner; public class SortStrings {     public static void main(String[] args)     {         int n;         String t...