Sum Of 2-D Array
Write a program to print the sum of all the elements of a 3x3 matrix.
import java.io.*;
class Sum
{
public static void main(String ar[])throws Exception
{
BufferedReader ob=new BufferedReader(
new InputStreamReader(System.in));
int i,j,sum=0;
int a[][]=new int[3][3];
System.out.println("Enter "+(3*3)+" elements ");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
a[i][j]=Integer.parseInt(ob.readLine());
}
}
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
sum=sum+a[i][j];
}
}
System.out.println("\nSum = "+ sum);
}
}
Have something to say? Log in to comment on this post.
0 comments