Sum of Diagonal elements



CODE:
import java.util.*;
class Diagonal
{
     public static void main(String args[])
     {
           int m,n,i,j,sum=0;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter no of rows & columns :");
           m=sc.nextInt();
           n=m
           int a[][]=new int[m][n];
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print("enter a no:");
                     a[i][j]=sc.nextInt();
                }
           }
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     if(i==j)
                     sum=sum+a[i][j];
                }
                System.out.println("Sum="+sum);
           }
     }
} 
OUTPUT:        
          

No comments :

Post a Comment