 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can calculate some of major and minor diagonals of a matrix using single loop. Before starting, you can note my information. You can subscribe my YouTube channel through this URL that is youtube.com slash sanjaygupta underscore tech school. You can download my programming app that is Techimus which is available on Google Play. Coming on to the topic, here you can see I have implemented some portion of the program that is in C programming. So first I have included a header file, then inside main function a 2D array is declared along with i, j variables and s which is initialized with 0. Then printf, this printf will print the message enter elements on console. Then with the help of this nested loop, I am going to receive all the elements of 2D array from user. Now after reading operation, first I am calculating some of major diagonal using single loop. So for that purpose, I am implementing a loop and here I am writing the statement as equals to s plus a of i, j sorry a of i, i. So this loop will calculate some of major diagonal. So major diagonal is also known as main diagonal. Main diagonal's indexes are same. Low index and column indexes are same. That's why I am writing i, i in both the indexes because i is containing 0 first time. So it will represent a 0 0. When i will be incremented to 1, then a will represent a 1 1. So this way all low and column indexes which are same, those elements will be added inside s. After this addition or after this completion of this loop, I can print sum of major diagonal on console. Now again I am re-initializing s with 0 and I am going to calculate sum of minor diagonal. For this I am writing s equals to s plus a of i and a of 2 minus i. So this statement will calculate sum of elements which are available at minor diagonal. So if you are talking about 3 by 3 matrix, so minor diagonal indexes will be 0 to 1 1 and 2 0. So for 0 2, I am writing i and 2 minus i. So i is 0. That's why it will provide 0 and 2 minus i. So 2 minus 0, it will provide 2. So first index will be 0 2. That will be added into s. Then i will be incremented to 1. So a i is 1. So it will provide 1 and 2 minus 1 is 1. So 1 1 index will be available here. Then third time i will be becoming 2. So a i is 2. So 2 and 2 minus 2 is 0. So third index will be 2 0. So this way all the elements of minor diagonal will be added to s. Now to print the result, again I am implementing printf statement sum of minor diagonal equals to percent d and then s and after that return 0. So this way I have calculated sum of major diagonal as well as sum of minor diagonal and you can see both are calculated with the help of single for loop. So instead of using nested loop, I have implemented single for loop to calculate sum of major diagonal and sum of minor diagonal. Now I am going to execute this code. You can see it is asking for elements. So I am entering the values. Now you can see the output. Sum of major diagonal is 15 and sum of minor diagonal is also 15. Now check 159 is representing major diagonal. So its sum is 15. So major diagonal output is correct. Now check the minor diagonal 357. So 357 is also equals to 15. If we add 357, so the result will be 15. This way you can see both the outputs are correct major diagonal and minor diagonal. So friends, you can implement sum of major and minor diagonal logic with the help of single for loop. As I have demonstrated you in this video, I hope you have understood this concept well how you can calculate these sum of major and minor diagonals using single loop. If you want to watch more programming related videos, you can visit my youtube channel through the link that is youtube.com slash sanjay gupta underscore tech school. You can also download my app based on programming that is tagmiz which is available on google play. So keep connected with me if you want to learn programming. Thank you for watching this video.