 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can calculate some of major as well as minor diagonal of a matrix. So I will be implementing the solution in C language and before start, I just want to share one information. If you go to detail or description of this video, you will find links of various players related to C language. So you can watch various programs related to C on those players. So do watch them. Now, first I'm going to tell you the logic through which we can calculate some of major and minor diagonals. And then I will be writing. So let's say this is a three by three matrix. These are the indexes. So no index 012 as well as column index is 012. And these are the values. So these nine values are available in the matrix. So if I want to add all these values, one, five and nine. So this will be called as major diagonal. This is major diagonal. And if I want to add these values, three, five and seven. So this is called as minor diagonal. So I will tell you how you can calculate some of both the diagonals with a single C program. So now I'm going to write the solution for you. So first of all, I'm going to declare a 2D array, then two variables ij for loop rotation. Then S1 equals to 0 and S2 equals to 0. So S1 will be having some of major diagonal and S2 will be having some of minor diagonal. Now a print S which will display the message and enter three by three matrix. And after this, I'm going to repeat two loops, one for rows and one for columns. So these are two loops and these loops will be reading values from user. So till here program is very simple. We just written the code through which we can read values from the user. Now I'm going to write the logic. So first I'm going to write the logic for adding major diagonal. So if we identified an index of major diagonal values, so one, five and nine. So if we see the index, so they are 00, 11 and 22. 00, 11 and 22. It means if row and column indexes are same, then we have the value for major index, right? And if I talk about minor index, if I talk about the value for minor index, so here it is three, five and seven. So if we see the index of three, it is zero and two, then for five it is 11 and for seven it is 20. So here what is the logic? If we add both, if we put plus here, so it will be two already. So it means in your matrix, if you have three rows or three columns, then you need to check some of those indexes, you need to check some of those indexes which are available on a minor diagonal position. If they are equal to a row size or column size minus one, then you can add those values as minor diagonal, right? And if you see other indexes like this 00, 10, 01, then for four it is 10, for eight it is 23, for nine it is 22, for six it is 12. So if you see other values, then their indexes are not making two, right? So now I'm going to write the logic. So this is first loop, then I'm writing second loop. So this is J, second loop, master loop. Now inside this, you can write if I double equals to J. So if I and J both are equal, it means we are talking about major diagonal. So here we can write S one equals to S one plus A of I J, right? So if both the indexes are equal, then only this value will be added to S one. So it means we are calculating major diagonal sum. Now if I plus J double equals to two, then this will be having those values which are on minor diagonal positions. Now remember that here we have dimensions as three by three. If you have dimension as four by four, five by five, then you will see the sum of minor diagonal values indexes will be low or column size minus one always. So if you have four by four matrix, then the sum will be three. The sum of those values which are on minor diagonal position, their indexes sum will be three in case of four by four matrix, right? In case of five by five matrix, their sum will be four. So this way you can easily identify like we have three by three dimensions. So that's why I J sum should be equal to two. If it is so, then we can add value of I J into S two. So first step is for main diagonal that is laser diagonal. Second is for minor diagonal. So here are two different logics are implemented. Now I can close this loop and close this right. And after closing this loop, I can write sum of minor diagonal equals two percent the sum of minor diagonal equals two percent the then S one comma. And then I can complete the main function. So this way I have implemented the complete solution in front of you. I explained you the logic for major diagonal indexes or minor diagonal indexes. So this way you can understand like how you don't need to remember the whole program. You just need to remember this logic. So how we can identify a common logic for major diagonal position indexes. So they will be equal for normal column and for minor for minor diagonal positions. You just need to add both the indexes I and J. If their sum is equals to sorry, if their sum is equals to a row or column size minus one, then the value is available on minor diagonal. Right. So I hope you understood whatever I explained in this video. And try to implement this in your system so that you can verify and test whether the code is implemented correctly or not. And if you want to watch more programming related videos, open my channel, go to playlist or you can go to detail your description of this video where you can find links of various players related to the program. So I hope you understood whatever I explained. Thank you for watching this video.