 Hello friends, I am Sanjay Gupta. I welcome you on my channel. Long programming to be, you can search my channel on YouTube. You can subscribe my channel for updates. It contains 800 plus videos. In this video, I am going to demonstrate you how you can calculate some of diagonal of 2D array in Java. First of all, I am going to import a package whose name is Util and inside this package a class is available scanner. So I am going to import this scanner class. That's why I have written this statement. Now I am going to define a class whose name is diagonal sum inside this class. I am going to define main function. So main function will be containing the complete code. So this is 2D array declaration. I am going to read values of rows and column from user that variable as now to receive input from user. First I need to create instance of scanner class with the help of this input I can receive inputs from user. So first I am going to display the masses, enter row and column size. So row size will be stored inside R variable and column size will be stored inside C variable. Now memory allocation so this way I have declared 2D array of RC size. So this statement will display the masses, enter elements. Now enter elements will be received with the help of these master loop. So line number 14 to 17 are responsible for reading input from user. After reading input I have to calculate some of diagonal elements. So again I am going to use these master loops. Here I am going to provide if condition if IW equals to J. So I is representing row index and J is representing column index. So if both are same it means they are representing diagonal position. So here I can write S equals to S plus ARR IJ. So IJ if both are equal it means diagonal elements are available. So those elements will be added into S variable. After completion of these master loops I can print some on output screen. So this way I have implemented the complete code in front of you. There is one more logic available which will calculate some of diagonal using single loop. So after execution of this code I will also demonstrate you how you can do that. So first I am going to execute this code. Here you can see I am compiling and executing the code. I am entering row size as 3, 3. Make sure that row and column size must be equal because diagonal is available in square matrix where rows and column size are equal. So I have to enter 9 elements. I have entered 1, 2, 3, 4, 5, 6, 7, 8, 9. See the output. So diagonal elements if we write 1, 2, 3. So in first row 1 is diagonal element. Then 4, 5, 6, 5 is diagonal element. Then 7, 8, 9. 9 is diagonal element. So 1 plus 5 plus 9 equals to 15. So this program is working properly. It is calculating proper diagonal sum. Now I am going to show you how you can calculate diagonal sum through one loop. So I am making this logic as comment. Now I am going to write another logic. Here you can see I am repeating only one loop and I am applying this logic. S equals to S plus ARRI. You can see I am using same index for row and column. So if you see the diagonal position 0, 0, 1, 1, 2, 2. So if matrix is of 3 by 3 then diagonal positions indexes are same. 0, 0, 1, 1 and 2, 2. So if I write same variable for row and column index then they will automatically provide diagonal position. So with the help of this single loop you can also calculate diagonal sum of 2 ARRI. So I have saved the code. Now again I am going to compile this code. I have updated that's why I am re-compiling it. After successful compilation I have to execute the code. Again I am going to enter row and column sizes 3, 3 and after that I have to enter 9 elements. So you can see the output. Again it is showing 15 S sum of diagonal. So it means there are 2 possible methods for calculating sum of diagonal of 2 ARRI. You can use nested loops as well as you can do that operation with the help of single loop. I hope you have understood whatever I have demonstrated you in this video. If you like this video you can subscribe my channel. You can follow me on YouTube. Thank you for watching this video.