Wednesday 11 June 2014

Addition Of Matrix in Java

import java.util.Scanner;


public class AdditionOfMatrix {

/**
* @param args
*/
public static void main(String[] args) {
     // TODO Auto-generated method stub
          int rows,columns,sum=0;
          int[][] matrix;
          Scanner input=new Scanner(System.in);
          System.out.println("enter the number of rows: ");
          rows=input.nextInt();
          System.out.println("enter the number of columns: ");
          columns=input.nextInt();
          matrix=new int[rows][columns];
          System.out.println("enter the elements of matrix : ");
          for(int i=0;i<rows;i++)
          {
               for(int j=0;j<columns;j++)
              {
                    matrix [i][j]=input.nextInt();
              }
          }
          System.out.println("Matrix is: ");
          for (int i = 0; i < rows; i++)
          {
                  for (int j = 0; j < columns; j++)
                  {
                          sum += matrix[i][j];
                          System.out.print(matrix[i][j] + " ");
                  }
                  System.out.println();
          }
          System.out.print("Sum of all elements is: " + sum);

}


}

No comments:

Post a Comment