Array-promo
Matrix-promo

Question

program to find sum of diagonal elements of matrix

				
					Matrix  = [9, 8, 7]
          [6, 5, 4]
          [3, 2, 1]

SUM OF DIAGONAL MATRIX:25
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SumOfDiagonalsOfMatrix  
{  
    public static void main(String[] args)
    {  
        int size=0,i=0,j=0,sum=0;  
        int a[][];
        
        
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE SIZE OF MATRIX");
        size=sc.nextInt();
        
        a=new int[size][size];
        System.out.println("ENTER THE ELEMENTS IN MATRIX");
        for(i=0;i< size;i++)
        {
            for(j=0;j< size;j++)
            {
                a[i][j]=sc.nextInt();
            }
        }
            
        

        for(i=0;i< size;i++)
        {
            for(j=0;j< size;j++)
            {
                
                if(i==j || (i+j==size-1))
                {
                    sum=sum+a[i][j];
                    
                }
                
                    
            }
              
        }
        System.out.println("SUM OF DIAGONALS OF MATRIX:"+sum);
            
    }  
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *