Printing the Values of a Row of a Two-dimensional Array in Java

Problem:
Create a two-dimensional array, fill it randomly with values between 0 and 99, and then print the sum of the values per row.

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++)
    {
      int sum = 0;
      for(int j =0;j<list[0].length;j++)
      {
        System.out.print(list[i][j] + "\t");
        sum+= list[i][j];
      }
      System.out.println("The sum of the row is " + sum);
    }
  }
}

No comments:

Post a Comment