 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you how you can print reverse of an array by swapping its elements. Before starting, you can note my information, you can follow or subscribe my YouTube channel through this URL youtube.com slash sanjaygupta underscore tech school. You can download my programming app TechMS which is available on Google Play. Now, coming onto the topic to implement this logic, I am going to implement a program in C which will print reverse of array by swapping all the elements of array. So, first I am including a header file, scdiv.h, then inside main function, I am going to declare an array variable i, j, and t. Then with the help of printf, I am going to display a message, enter elements. To receive elements of array, I am implementing for loop and inside this for loop, I am applying scanf statement which will receive 10 elements and all the elements will be stored inside a array. Now for swapping the elements of array, I am again starting a loop. Here I am initializing first variable i from 0 and another variable j with 9. So, you can see I have initialized two variables i from 0. So, it will be pointing first element of the array and j from 9, it will be pointing to the last element of array. Here condition is i less than j. So, loop will repeat when i is less than j. If i is equal to j or greater than j, then this loop will be terminated. Then i plus plus comma j minus minus. So, I will be swapping the elements of array. So, if array is containing 10 elements, so I have to swap beginning 5 elements with the last 5 elements. So, if I have total 10 elements, then I have to repeat the loop only 5 times because at one rotation, 2 elements will be interchanged. At second rotation, next 2 elements will be interchanged. So, if I have 10 elements, then only 5 rotations are required to swap the complete array. So, for that purpose, I have implemented this condition. If i is less than j, then only loop will repeat otherwise it will be terminated. Now, I am going to apply swapping statements so that I can interchange the values of array. So, t equals to a of i because ai, i is 0, so it is pointing to first location. Then ai equals to aj. So, ai is pointing to first location and aj is pointing to last location. Then aj equals to t. So, this way I have interchanged value of ai and aj. So, first time i is representing 0 index, so first element and j is representing 9th index, that is last element. So, 0 and 9th index are swapped. Then i plus plus, so i will become 1 and j minus minus, j will become 8. So, 1 and 8 index values will be swapped and so on. So, after this swapping operation, I can print this message, reverse of array on console using printf. And then with the help of this loop, I am going to print the elements on console and then at last return 0. So, this way this first segment of program is reading the array. Second segment of the program is swapping all the elements of array so that we can print its reverse. And third segment of the program is printing array on console. Now, I am going to execute this code. You can see it is asking for 10 elements. So, I am entering 10 elements starting from 1 to 10 and you can see the output. Reverse of array, it is printing reverse of array properly. So, this way by swapping all the elements, I am printing the reverse of array. So, I have entered 1, 2, 3, 4, 5 up to 10 and the output was 10 to 1. So, this way by swapping physically the elements of array, you can reverse all the elements of array. Now, I am going to remove this j from this loop and I am applying condition as 10 by 2 because if number of elements are 10, so I have to repeat it only 5 times. In place of j, I am applying 9 minus i here. So, without using j variable, we can also swap the locations of array beginning and ending. So, here you can see i is representing 0. So, a 0 location and a 9 minus i. So, i is 0. So, it will provide ninth indexed value. Then, i plus plus will be performed. So, when i will be incremented by 1. So, a i means a 1 location and a 9 minus i means 9 minus 1. So, it will be a 8. So, this way by using only i, we can also print reverse of array. Again, I am executing this code. I am entering 10 elements and you can see the reverse of array is printed on console. So, with this way I have demonstrated you two different ways how you can print reverse of array by using two indexes or you can only use single index so that you can manage both the indexes. Now, this logic is implemented using third variable. We can convert this logic without using third variable also. So, now I am going to change these three statements so that we can apply this logic without using third variable. So, for that purpose I am writing the code a of i equals to a of i plus a 9 minus i. Then, a 9 minus i equals to a of i minus a 9 minus i. And third statement a of i equals to a of i minus a 9 minus i. So, it is equivalent to this a equals to a plus b. It is equals to b equals to a minus b and it is equals to a equals to a minus b. So, this is a simple logic which we apply on two variables. Here you can say a of i is representing a and a 9 minus i is representing b. I have applied the logic here. I have to use minus sign by mistake. I have used equals to. So, now I am again executing this code so that you can see whether it is printing reverse or not. You can see the output. The elements are printed in reverse order. So, this way I have demonstrated you different concepts that you can use to print reverse of an array by swapping physical locations of the elements. I hope you have understood all the logics which I have demonstrated you in this video. If you want to watch more programming related videos, you can follow or subscribe my YouTube channel through the URL. That is youtube.com slash sanjaygupta underscore tax school. You can also download my programming app TakMS which is available on Google Play. So, keep connected with me. If you want to watch programming related videos or you want to learn programming. Thank you for watching this video.