Wap To input numbers into a array of 3 rows And 3 coloumns . find the sum of the numbers in the diagonal elements . no element should be repeated twice
Evergreen Publication Model paper 1 Q8
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];
if(i+j==2&&i!=j)
Sum = Sum + a[i][j];
}
}
System.out.println("Sum of Diagonal = "+Sum);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment