 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can calculate some of main diagonal of a matrix using single for loop. Before starting, you can note my information. You can visit or subscribe my YouTube channel through the link youtube.com slash sanjaygupta underscore tech school. You can download my programming app Tagimace which is available on Google Play. Now coming on to the topic. In this video, I am going to implement a program in C that will calculate some of main diagonal of a matrix using single loop. So first I am including a header file that is stdio.h then inside main function I am declaring a 2D array of size 3 by 3 then ij which will represent rows and columns and then s equals to 0. Now I am going to print a message through printf enter elements. To read 2D array I am going to implement combination of nested loops. So this way I have implemented combination of nested loops so that elements of matrix can be received from user. So you can see first loop is starting from 0 its condition is i less than 3 and increment is i plus plus so this loop will repeat 3 times. Inside this loop another for loop is available which will also repeat 3 times because the dimension of 2D array is 3 by 3 and this scanner will receive all the elements which are entered by the user. Now after reading these elements I have to calculate some. So to calculate some I am implementing a for loop so this for loop will repeat 3 times and it will calculate some of elements which are available at main diagonal. So here you can see this logic loop is started from 0 and condition is i less than 3 so here s equals to s plus i i is available. So you can see row and column index both are having i so initially i is 0 so it will provide a 0 0 so a 0 0 index will be added in s then i will be incremented by 1 so it will become 1. So next time a i i means a 1 1 so 1 1 index will be added to s so this way all the diagonal elements all the main diagonal elements that are 0 0 1 1 2 2 will be added to s. So if you if you notice that main diagonals row and column indexes are equal that are 0 0 1 1 and 2 2 that's why I have implemented this logic to calculate some of the elements which are available at main diagonal. After completion of this loop I can print the result on console using this printf and then return 0 so this way first I have scanned all the elements inside matrix then I have calculated some of diagonal and then I have displayed the result on console with the help of s variable. Now I am going to execute this code so it is asking elements so I am entering these values you can see the output sum equals to 15 so now check the main diagonal that is 1 5 and 9 so 1 plus 5 plus 9 is equals to 15 so this way you can see the output is correct. So this program is working properly it is calculating some of main diagonal only using this single loop. I hope you have calculated some of diagonal with nested loops by checking if condition if i w equals to j but you can also do this with this single loop so remember that you can calculate some of main diagonal using nested loops as well as using a single for 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 download my android app based on programming that is TechMS which is available on google play so keep connected with me learn programming because programming is very easy. Thank you for watching this video.