Advertisement
Guest User

Java

a guest
Feb 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Passing {
  4. public static void main(String[] abs){
  5. int[][] m = getArray();
  6. System.out.printf("Sum of Array is: %d\n", +sum(m));
  7. }
  8. public static int[][] getArray(){
  9. System.out.println("Enter 2D array's values:-");
  10. Scanner in = new Scanner(System.in);
  11. int[][] m = new int[2][2];
  12.  
  13. for(int i=0; i<2;i++){
  14. for(int j=0;j<2; j++) {
  15. m[i][j] = in.nextInt();
  16. }
  17. }
  18. return m;
  19. }
  20. public static int sum(int[][] m) {
  21. int total = 0;
  22. for(int i=0; i<2; i++) {
  23. for(int j=0; j<2; j++) {
  24. total = total+m[i][j];
  25. }
  26. }
  27. return total;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement