 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can calculate sum of diagonal of 2D array. So I will be explaining two methods. First will be using two loops or using nested loops and second method will be using only one loop. So with these two ways, you will see how we can calculate sum of diagonal of 2D array or matrix. So before starting the explanation, just one update. If you go to detail or description of this video, you will find links of various playlists related to C language. So you can follow them. Now I am going to explain this first way that is using two loops or using nested loops. So let's say we have a 2D array whose dimension is 3 by 3. So remember one thing, if you are going to calculate sum of diagonal, so the dimension of your matrix will be same. Low and column size will be same. Then you can calculate diagonal sum. And these are other variables that I am going to use. Then the sprint net will display the masses and the 3 by 3 matrix on output screen. So these two loops will be reading values from user. So this is the reading part of 2D array. So after this reading part, now I am going to explain the second part that is to calculate sum of diagonal using two or nested loops. So I am starting one more part of loop. This is first loop that is for rows and this is second loop that is for and inside this I am going to write the logic. So let me first draw a matrix. So let's say this is 3 by 3 dimension matrix. So these are the indexes 0, 1, 2 row index and 0, 1, 2 column index and let's say these are the values 1, 2, 3, 4, 5, 6, 7, 8, 9. So we need to calculate some of these values 1, 5 and 9. Now if you notice that 1 is available at 0, 0 index. So 1 is available at 0, 0 index. Then 5 is available at 1, 1 index and 9 is available at 2, 2 index. So if we see all the elements which are on diagonal position having same row and column indexes. So inside this loop, I am going to apply a market condition that is if Iw equals to j. So I is representing row index and j is representing column index. So if both are equal, then I can apply as equals to s plus a of ij. So this way all the elements which are available on diagonal position will be added. And after completion of this loop, after completion of this loop, I can print the term on output screen with the help of this printf. So this way this is the first method which is using two or nested loop that I explained you. So in this method we compare both indexes i and j. i is representing row index, j is representing column index. If both are same, then only these elements will be added otherwise not. Now I am going to implement this method that is using one loop. So I am going to convert this implementation to one loop only. And you will see we will be able to have addition of diagonal. So I am removing these statements. So here you can see we have only one loop. We have only one loop and here I am writing these. So this is only one loop and with this one loop I am able to add the locations or the values which are available on diagonal positions. So now iterate this loop with me. i is 0, i is less than 3 that is true. Now check this a i i on both index and hk i. So i is 0, so it will become a 0 0. So a 0 0 index will be added to s. Then i will be implemented to 1. So 1 is less than 3, it is true. Then indexes will be a 1 1. So we require 0 0 1 1 2 2. So we are putting same values on both row and column index. So if we put same values on both the positions. So automatically we will fetch diagonal positions of 2D array. So those will be added to s. After completion of this loop, some will be printed on our code screen with the help of s. So this way I hope you understood how we can calculate some of diagonal elements using simple loop only. So this is very important. You should know how we can process diagonal using one loop only because row index and column indexes are same. So don't use two loops to calculate some of diagonal. You can use simple loop to do that operation. So I hope you understood both the ways like how we can calculate some of diagonal using master.tool loops or how we can calculate some of diagonal using one loop only. So I hope you understood whatever I explained in this video. If you want to watch more programming related videos, you can open my channel, go to playlist or you can go to detail or description of this video where you will be having links of various playlist related to C language. So do follow them. Thank you for watching this video.