Computer Science, asked by honorbeep708, 8 months ago

write a program to accept data in 3x3, 2-D array and find the sum of all data java method ​

Answers

Answered by vidit05gupta
1

Explanation:

To sum all the variables in the 2D array, take the following steps -

  • Import Scanner module to use the Scanner class.
  • Declare 2 variables - i & j to iterate the loop.
  • Declare 1 variable - sum to calculate the sum with the value 0.
  • Declare 1 2D array.
  • Input the values inside the two loops used.
  • Inside the same loop, calculate the sum.
  • Finally, print the sum.

Program:

import java.util.Scanner; // access Scanner class

public class Main

{

  public static void main(String args[])

  {

   // initializing variables

      int i, j, sum = 0;

      int arr[][] = new int[3][3];

      Scanner scan = new Scanner(System.in);

   

      // inserting the array elements

      System.out.println("Enter the Array Elements : ");

      for(i = 0; i < 3; i++)

      {

          for(j = 0; j < 3; j++)

          {

              // inserting the values here

              arr[i][j] = scan.nextInt();

               

              // calculating the sum here

              sum = sum + arr[i][j];

          }

      }

   

      // the printing inserted array

      System.out.print("The Array is :\n");

      for(i = 0; i < 3; i++)

      {

          for(j = 0; j < 3; j++)

          {

              System.out.print(arr[i][j] + "  ");

          }

          System.out.println();

      }

       

      // dispaying the sum

      System.out.print("The sum is : " + sum);

  }

}

#answerwithquality

#BAL

Attachments:
Similar questions