Wednesday, 13 May 2020

Find the sum of diagonal elements in Java

Write a program to input and store integer elements in a double dimensional array of size  3 x 3  and find the sum of diagonal elements. 

import java.util.*;

class DDA
{
    public static void main(String arg[])
    {
        int[][] num = new int[3][3];
       
        int x = 0 ;
       
        Scanner sk = new Scanner(System.in);
   
        System.out.println("Enter all the values ");
       
        for(int i=0 ; i<3 ; i++)
        {
            for(int j=0 ; j<3 ; j++)
            {
                num[i][j] = sk.nextInt();
            }
        }
       
        x = num[0][0] + num[1][1] + num[2][2] + num[0][2] + num[2][0] ; 
       
        System.out.println("The sum of the diagonal elements is : " +x) ;
   
    }
}

====================================================

Search my YouTube channel   :    SK Skill India 

====================================================








No comments:

Post a Comment