Social Icons

Saturday 21 September 2013

Sum Of all cross diagonals of an array

import java.util.Scanner;
class Array
{
public static void main(String args[])
{int l,k;
int count=0,sum=0;
 int a[][]=new int[4][4];
 int i,j,c;
Scanner s= new Scanner(System.in);

System.out.println("Enter element Of the array : ");
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
a[i][j]=s.nextInt();
}

}

System.out.println(" Element Of the array are : ");
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println("");

}

l=a.length-2;

System.out.println("Sum of the Diagonals Element Of the array are : ");
System.out.println("element at the  left    corner is  : "+a[a.length-1][0]);
for(c=l;c>=0;c--)
{
for(i=0;i<a.length;i++)
{
for(j=0;j<a.length;j++)
{

   if(i==(j+c))
   {  
   count=count+a[i][j];
    }
 
 }
 }

 System.out.println("Sum of diagonal starting at row "+"is:"+count);



count=0;

}


for(c=1;c<a.length-1;c++)
{
for(i=0;i<a.length;i++)
{
for(j=0;j<a.length;j++)
{

   if(i+c==j)
   {  
   count=count+a[i][j];
    }
 
 }
 }

 System.out.println(" Sum of diagonal starting at row "+"is :  "+count);



count=0;

}


System.out.println(" element  at  the   right corner is: "+a[0][a.length-1]);

}
}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management