 Hello friends, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can check a number for Palindrom. Right? So I will be explaining how to read a number. Then I will tell you how you can check whether that number is Palindrom or not. So before starting, I want to share one information with you. So if you go into the detail of this video, you will find links of various playlists which are related to different topics of C programming. So you can pause them. Now coming onto the topic. So I will be explaining this with the help of C programming. So here you can see two scenarios are mentioned. So first is number is 123. Now we need to identify its reverse so that we can check the Palindrom. So its reverse is 321. So in case of Palindrom, if original numbers reverse is equivalent to that number, then that number is known as Palindrom. So here 123, its reverse is 321. So 321 is not equal to 123. So that's why it is not a Palindrom. Then here number is 121. Its reverse is 121. So both are equal. It means this number is a Palindrom. So now we will implement this program in two parts. First, the reverse of the number will be calculated and then the reverse will be compared with the original number so that we can check whether it is Palindrom or not. So now I'm going to start writing the program. Then I will explain each and every step with the help of these two examples so that you can understand well how it is working. So here you can see I have declared four variables and will be the original number. REV will be storing the reverse of the number. Then two more variables are there T and A. So use of these two variables will be explained further. Now with the help of printf, this message will be displayed on an output screen that is to read a number. So enter a number, this will be displayed. Then with the help of scanners, I'm reading that number, the input number into n variable. So the number which is entered by the user will be stored inside this variable. Now I'm going to write the logic so that I can calculate the reverse. So I'm implementing while n greater than zero. Now inside this while I'm going to write the logic. So first time writing all the statements, then I will explain them one by one. So this is the logic which will calculate the reverse. This is for calculating the words. So now, first of all, I'm going to explain this so that you can understand how we are performing reverse of a number. So let's say I'm taking example as number is one point. So here I am writing the variables. So initial value of this number is one point. Now I'm going to rotate the scope. So number is greater than zero condition is true. Then we need to calculate this. So if we calculate this, so here you can see n modulus 10. So n is one point. When we take modulus of any number with 10, so it always provides last visit of the number. So new value of a will be three. Then here you can see I'm multiplying reverse into 10 and adding a into it. So right now you can see reverse is not having any value and we are multiplying it with 10. So if a variable is not having any value and we multiply it with any digit, so it will provide garbage value. So we need to avoid this. So here I am putting this reverse equals to zero. So initial value of reverse is zero. Now zero into 10, zero plus a. So what is the value of a? It is three. So new value of reverse will be three. Then n equals to n by 10. So we need to divide n by 10. So initial value of n is 133 if we divide it by 10. So the main remaining will be 12. Because whenever you divide any number to 10, let's say 123. So it will be this 12.3. And you can see all the variables are of type integer. So integer don't store this value, which is available after point. So this will be removed and only 12 will be stored inside and so this is the first rotation of this one. Now, again, we need to repeat this. So right now minus 12, 12 is greater than zero, then 12 modulus 10. So the remainder will be two. That will be stored inside a. So new value of a is two. Then calculate this right now. Current value of reverse is three. So three into 10 is 30 and add a. So a is two. So it will become 32. So new value of reverse is 32. And then again divide n by 10. So it's new value will be one. So if we divide 12 by 10, so answer will be 1.2. And because of it, we can have only one. Again, check the condition. It is again true. So again, n modulus 10. So n is one. One modulus 10 will provide maintenance one. Then reverse is 32. So 32 into 10 is the 20 plus a. So a is one. So it will become 321. So new value of reverse will be 321. Then divide n by 10. So n is one. So if we divide one by 10, so it will become zero on something. So n will be having zero. So this way, if next time we iterate this loop, so n is zero, zero is not greater than zero. So this condition will be one. And after termination of this loop, now you can see in reverse variable, we have reverse of the original number. So original number was 123. Now we have reverse as 321. So now you can compare this 321 with this n variable. So after completion of this loop, we need to write one if condition. So here we can write this if n w equals to reverse. So this statement will come just after completion of loop. Then if it is true, you can print, you can print Palindrom. If it is false, then you can print not or you can write a complete statement not Palindrom. And then you can finish your main function. So this way it is completed now. So let's check. There is a simple glitch that I want to resolve now. So after completion of this loop, we need to compare n with reverse. So reverse, you know, it is 321 and if we see the value of n, so it is right now zero. So after completion of while loop, after completion of this while loop, reverse will be having reverse of original number, but n will automatically become zero because we are dividing n by 10 continuously inside the loop. So can we use n here? No, because n is always zero. So it will always print one or not Palindrom. So now what is the solution to this problem? So before starting this loop, we need to take backup of n. So that's why I use this T variable here. So before starting the loop, I am assigning n into T. So it means before starting on this loop, variable n was having 123. So 123 will be assigned into T. And then inside this loop, we are just dividing value of n by 10. So n will decrease to zero, but its original value will be saved as a copy into T. So here in place of n, you can use T to compare it with reverse. So T has original value that is the input provided by the user, REV or reverse variable is adding the reverse of that number that was available inside the end. So if now this condition is true, then it will display Palindrom otherwise not Palindrom. So right now, if we see first example, so T will be having 123 and reverse will be having 321. So both are unequal. So it will print not Palindrom. But if n is 121, so its reverse will be 121. So it will display Palindrom. So this way I hope you understood how we calculated whether a number is Palindrom or not. So first we converted the original number into its reverse and then we compared both actual number and its reverse. If they are equal, then number will be Palindrom otherwise it is not Palindrom. So I hope you understood whatever I explained in this video. If you want to watch more programming related videos, you can go to my channel, open playlist so where various playlists related to various programming activities are available and I uploaded more than 1000 videos. So do follow my channel so that you can learn programming in easy way. Thank you for watching this video.