Wednesday, January 16, 2008

[2D array] Sum of left Diagonal

Wap a program to find the Sum of the Left diagonal of a 2D array

import java.io.*;
class SumDiagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][3];
int Sum=0;
System.out.println(" Enter 6 elements for array one :");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println(" Matrix ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
Sum = Sum + a[i][j];
}
}
System.out.println("Sum of left Diagonal = "+Sum);
}
}

No comments: