Summing all the Values in a Two-dimensional Array after Printing it in Java

Problem:

Create a two-dimensional array, fill it randomly with values between 0 and 99, calculates the sum of all the numbers and then print it as the picture shows.

Output:



Solution:

public class twoDimentionalArrays
{
  public static void main (String[] args)
  {
    int[][] list = new int[10][10];
    for (int i =0;i<list.length;i++)
    {
      for (int j=0; j<list[0].length;j++)
      {
        list[i][j] = (int) (Math.random()*100);
      }
    }
    
    for (int i =0;i<list.length;i++)
    {
      for(int j =0;j<list[0].length;j++)
      {
        System.out.print(list[i][j] + "\t");
      }
      System.out.println();
    }
    int sum =0;
    for (int i =0;i<list.length;i++)
    {
      for(int j =0;j<list[0].length;j++)
      {
        sum+=list[i][j];
      }
    }
    
    System.out.println("The sum of all values in the two-dimensional array is " + sum);
  }
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact