Sum of Left and Right Diagonal Elements of a Matrix Best Trick | topperbhai.com

Sum of left diagonal and right diagonal elements of a matrix in java

Sum of Left and Right Diagonal Elements of a Matrix


 
In this tutorial, we will learn to write a java  program to find the sum of left diagonal and right diagonal elements of a matrix.

 

Note:

Sum of left and right diagonal elements of matrix means you have to access all the left diagonal and right diagonal elements of the input matrix to calculate their sum and store the sum of both diagonals in separate variables. 

 

If you are  not able to understand the above definition,  then look at the infographic given below. 



Sum of Left and Right Diagonal Elements of a Matrix






Below, In this post, I have  written a little bit long but  easy-to-understand java program which accepts elements of  a matrix from the user using scanner input and prints the  sum of left diagonal and right diagonal elements of the input matrix separately. 


 

The logic I have implemented in this program is pretty easy to understand.


Explanation of the Code:

  • The code is a program that asks the user to enter the number of rows and columns.

  • The code then creates an array with two dimensions, one for each row and column.

  • It also prints out some text on the screen before it starts looping through all of the elements in its arrays.

  • The first thing that happens is that we create an int variable called sumofleft which will store how many left diagonal elements there are in our matrix.

  • Then we create another int variable called sumofright which will store how many right diagonal elements there are in our matrix.

  • Next, we print out some text on the screen saying "Enter the number of Rows" followed by "Enter the number of Columns".

  • After this, we use nextInt() to get input from our user about what their numbers are for both rows and columns so they can be stored into variables r and c respectively (r stores how many rows there are while c stores how many columns).

  • We then use these variables to initialize arr[][] which is going to be used as a 2D array where arr[i][j] represents one element in our matrix at position i*c+j .

  • Finally, after creating this 2D array, we start looping through all of elements.

  • The code begins by defining variables for both the left and right diagonal elements.

  • Then, it sets up an array of integers that will hold all of the rows and columns.

  • Next, it prints out some information about what input to expect from the user before proceeding with any calculations.

  • After this, it loops through each diagonal in order to calculate their respective sums.


 

Sum of Left and Right Diagonal Elements

 import java.util.*;  
 public class Sum_of_Right_and_Left_Diagonal  
 {  
   public static void main()  
   {  
     int sumofleft=0, sumofright=0;  
     Scanner sc=new Scanner(System.in);  
     System.out.println("Enter the number of Rows");  
     int r=sc.nextInt();  
     System.out.println("Enter the number of Columns");  
     int c=sc.nextInt();  
     int arr[][]=new int[r][c];  
     System.out.println("Enter the elements of Matrix");  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
         arr[i][j]=sc.nextInt();  
       }  
     }  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
         System.out.print(arr[i][j]+" ");   
       }  
       System.out.println("");  
     }  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
        if(i+j==r-1 && i==j)  
         sumofleft=sumofleft+arr[i][j]; 
sumofright=sumofright+arr[i][j];
} } System.out.println("Sum of Left Diagonal Elements="+sumofleft);
System.out.println("Sum of Right Diagonal Elements="+sumofright); } } ------------ OUTPUT:- ------------ Enter the number of Rows 3 Enter the number of Columns 3 Enter the elements of Matrix 1 5 9 8 7 5 3 2 4 

 1 5 9 8 7 5 3 2 4
Sum of Right Diagonal Elements = 19
Sum of Left Diagonal Elements = 12 -----------------------------------

Post a Comment

0 Comments