Dremendo Tag Line

Input numbers in 2d array and find sum of all numbers in Java

Two Dimensional Array - Question 1

In this question, we will see how to input numbers in a 3X3 integer matrix (2d array) and find the sum of all the numbers in Java programming. To know more about two dimensional array click on the two dimensional array lesson.

Q1) Write a program in Java to input numbers in a 3X3 integer matrix (2d array) and find the sum of all the numbers.

Program

import java.util.Scanner;

public class Q1
{
    public static void main(String args[])
    {
        int a[][]=new int[3][3], r,c,s=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 9 numbers");
        for(r=0; r<3; r++)
        {
            for(c=0; c<3; c++)
            {
                a[r][c]=sc.nextInt();
                s=s+a[r][c];
            }
        }
        System.out.println("Sum of all the numbers = "+s);
    }
}

Output

Enter 9 numbers
12
8
13
4
34
17
15
32
60
Sum of all the numbers = 195
video-poster