 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can print reverse of a number in C programming. So first I am going to tell you how you can build the logic and then I am going to tell you how you can convert that logic into a C program. So starting with it, so let's say we have a number 123 and we need to print its reverse. So its reverse will be 321. So now I am going to tell you how you can build the logic and then I will be converting that logic into a program. So to implement this logic, I need to separate all three values. So 1, 2 and 3. I am going to separate them and then I will calculate the reverse. So first of all I want this 3 from 123. So for that I am using this statement and modulus 10. So this is modulus operator which calculates remainder value. So it always calculates the remainder value and whenever you use this with a number and you divide that number with 10. So remainder will be always the last digit of the number. So this is a mathematical rule. So I am assigning that number into it. So whenever you want last digit of a number then you can simply divide that number using this modulus operator along with 10. So this a will receive the value 3. So right now value of a is 3 and 10 was 123. Now we have 3. So in next step I am going to calculate s equals to s into 10 plus 8. Why so? Because every time whenever I will be having the values. So I need to put it like this. So first it will be 3 then I will convert it into 32. Then I am going to convert 32 into 321. So every time I need to multiply the outcome with 10 and need to add the calculated remainder into that value. So first time I only want 3. So initially I am assigning 0 into s. So 0 into 10, 0 then we are adding a. So a current value is 3. So final outcome of s will be 3. So I am repeating it. Initially s is 0. 0 into 10 is 0 plus a. So a is 3. So 3 will be added to this expression. And the outcome of s will be 3 till here. Now we calculated 3. So if we remove this 3 from this number. If we remove this 3 from this number. So we received 12 only and we want this because 3 we already calculated. So now again very simple logic. Whenever you want to remove a number that is available on the last of that number then you can simply divide that number with 10 and store that into that variable. So here n was 123. I divided it with 10. So it will become 12.3 and all the variables are of integer type. So this point 3 will be removed and new value of n will become 12. Right. So whenever you divide any number with 10. So its last digit will be removed and number will be. So in our case number will be 12 because 3 is removed. Now after these 3 statements I am going to implement first statement again. So here you can see I am again dividing n with 10 so that I can calculate the integer value. So current value of n is 12. So 12 modulus 10. So again that rule whenever we use modulus operator. So if we divide a number with 10. So inventory will be always last digit. So this time a will receive 2 or here value of n was 12. So 12 modulus 10 a will be receiving 2. Now we can implement this. So this time you will get to know why I wrote this statement. So again I am using it here. So now you need to find out the current value of n. So if we go above. So here s was 3. So we need to use 3 here. Then we need to multiply it with 10. So 3 into 10 is 30. Then we need to add a. So a's current value is 2. So the outcome of this will be 32 and that will be assigned into s. So right now we are calculating these steps in one go. So the previous value of variable you can use in the next expression and calculate the result. So let's say s is now 32. So if we use s afterwards from this statement. So value of s will be used as 32 not 3. So I hope till here you have understood. So now again we can remove 2 from that number because 2 we already calculated. So this time it will be n equals to n by 10. So if we divide 12 by 10, so it will become 1.2. So 0.2 will be removed automatically because number is off time. So n will be having the value 1. Now again you need to repeat all these 3 steps. So a equals to n modulus 10. So right now value of n is 1. So this 2 is removed because of this statement. So value of n is 1 and we are applying modulus. So here you need to remember some rule if we are using modulus operator and numerator value is less than denominator. So in our case we are doing this 1 modulus 10 numerator is 1 and denominator is 10. So thumb rule says result will be always numerator value. So a will be decision 1. Now again operator statement s equals to s into 10 plus a. So now we need to identify value of s. So here you can see if we go above. So current value of s is 32. So we need to multiply 32 into 10. So result will be 320. Then we need to add a. So last value of a is 1. So 320 plus 1. So outcome will be 321. And lastly I need to implement this statement again. That is n equals to n by 10. So current value of n is 1. So 1 by 10 if we divide any number with 10 and number is less than 10. So in case of division result will be always 0. Result will be always 0 because if we use integers so its value will be 0 or something. But in case of integer we only consider the value which is before decimal point not after decimal point. Result will be now 0. So if n is having 0 so we cannot calculate it further. So this is the logic that I explained right now to calculate reverse of this number 123. So you are thinking like where is the reverse. So here you can see. Here is the reverse. 321 which is stored inside s. Now you will say what if number is this 1, 2, 3, 4. So now you can see carefully. If I group these statements. So I wrote these statements 3 times only because my number was 120. So if number is 120 or you can say number is 3 digits. I mean 3 digits. So you need to write these 3 statements 3 times. If you have 4 digit number you need to write these statements 4 times. If you have 5 digit number you need to write these 3 statements 5 times. Now you will say if we have 10 digit number so do we need to write these statements 5 times. No. Now comes loop. So we don't need to write these statements again and again. We will pick only this block and we will repeat this block with the help of loop so that we can calculate the reverse. So I think you now got to know how we can create a logic so that we can print reverse of a number. So now I am going to convert this logic with the state program so that you can implement the program in your compiler and you can read the number from user and you can print its output in reverse form. So I am erasing these statements and I am going to implement a key program. So now I am going to implement a program. So starting with name I need variable n to read a number from user then variable a to store the modules value and variable s which will be having the reverse of the number. Then printf which will receive a number from user then scanf or send the n percent n. So the input will be stored inside this n variable. Now if we recall that logic that we implemented. So three steps we calculated that logic. Why so? Because finally n became zero. So now we need to implement three statements for loop. Those are initial value, condition and increment or increment. Like these three things we need to implement. So now you can identify the condition. We need to repeat the loop till n is greater than zero. If n is zero then we need to stop the loop. So here I am going to use value and I am writing the condition as n greater than zero. So if n is greater than zero then only this loop will repeat otherwise it will be permanent. Now you will say where is initialization. So here you can see this scanf. Canf is reading value from user into n. So this will be treated as initialization. So knowingly I am not using for loop here because in for loop we need to write initialization, condition and increment decrement in a single line. But here you can see initial value is received through scanf which is written before the loop. Then inside loop we just want to mention the condition and decrement will be if you remember the logic we used n equals to n by 10. So we remove each digit from the last of n through dividing it by 10. So that is decrement. So here I need to write it. So this will be the decrement. And above that I am writing this and this. So now these three statements will repeat till n is greater than zero and if n is equals to zero then this loop will be terminated. And just after this you can print reverse equals to for sending ns. So reverse will be stored inside s variable and that will be the reverse. Now remember that if you have taken this number, so reverse will be 5, 4, 3, 2, 1. So remember that if you are using n so its output will be garbage because range of n is 3, 2, 7, 6, 7. So if you exceed that limit then number will automatically convert into garbage. So instead of n what you can use? Long n. So remember this thing. Now I am going to iterate this quickly so that you can understand how this logic will calculate the outcome. So this time I am taking number as this 1, 2, 3, 4, 4 digit number and you will see this logic will calculate some of 4 digit as well as any number of digit that is having in a number. So if we took 2 digit number then also it will work. If we take 3, 4, 5, 6, any digits it will always work properly. So here you can see variable n is having 1, 2, 3, 4 and s is initially zero. Now we go to check this condition and greater than zero. So right now it is true. So we need to calculate this. So a will be having limiter that is last digit because we are dividing in fact n and using modernness. So it will give us 4 last digit which will be stored inside a. Now calculate this s is initially 0, 0 into 10, 0 plus a. So what is the current value okay? That is 4. So s will also be 4 because 0 into 10, 0 and plus 4 will be 4. Now we need to divide n by 10. So n initial value is 1, 2, 3, 4. So it will be now 1, 2, 3. Now again we need to check the condition. It is true. Then take modulus of n. So n's current value is 1, 2, 3. So we need to divide it by 10 and we need to take the limiter value. So now you can see limiter will be 3 because last digit is 3. So current value of a is 3. Then a current value of s is 4. So 4 into 10, 40 plus a is 3. So it will become 43. So current value of s is 43 and now we need to calculate this. So n will be having 12 because we are dividing it by 10. So limiter will be 12. Again check the condition. It is true. Calculate value okay. So it will be 2 because we need to take the limiter. Current value of s is 43. So 43 into 10, 430 plus a. So it will become 432. Now n equals to n by 10. So 12 by 10 it will be 1. Again check the condition. It is true. Then 1 modulus 10. So I told you if numerator is less as compared to denominator if you are using modulus operator. So limiter will be numerator. So it is 1. Current value of s is 432. So 432 into 10. That will be 4320 plus a. So it will become 4321. And n by 10. So it will become 0. Now if you check the condition it will be false. And we have reverse into s variable after completion of this loop that will be written on output screen. So this way I told you how we can calculate, sorry, how we can build the logic to calculate reverse of a particular number. No matter how many digits it contains. And then I told you how we can convert that logic into a C program. So I hope you understood how we implemented this program with f of C language. And you understood how we can print reverse of a number in C programming. So I hope you understood whatever I explained in this video. If you want to watch more programming related videos you can open my channel and go to playlists. So I uploaded more than 1000 videos on C, C++, Java, Salesforce, Python, data structure. So do follow my channel, subscribe it and keep learning programming because it is very easy. So thank you for watching this video.