Advertisement
AlfonsoPEREZ

ARRAY SUM JAVA APPLICATION

Nov 24th, 2022
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class JavaApplication1 {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.        
  9.         int sum_row = 0;
  10.        
  11.         int row = 3;
  12.         int column = 3;
  13.        
  14.         int[][] arr = new int[row][column];
  15.        
  16.         //Gets user input
  17.         for (int i = 0; i < row; i++){
  18.             for (int j = 0; j < column ; j++){
  19.                 System.out.println("arr[" + i + "][" + j + "] = ");
  20.                 arr[i][j] = sc.nextInt();
  21.             }
  22.         }
  23.        
  24.         System.out.println();
  25.        
  26.         //Outputs array
  27.         for (int i = 0; i < row; i++){
  28.             for (int j = 0; j < column ; j++){
  29.                 System.out.println("arr[" + i + "][" + j + "] = " + arr[i][j]);
  30.             }
  31.         }
  32.        
  33.         System.out.println();
  34.        
  35.         //Prints sum of row
  36.         for (int i = 0; i < row; i++){
  37.             for (int j = 0; j < column ; j++){
  38.                 sum_row += arr[i][j];
  39.             }
  40.             System.out.println("Sum row " + i +": " + sum_row);
  41.             sum_row = 0;
  42.          
  43.         }
  44.        
  45.     }
  46.    
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement