Hey, guys, what's up?
So today we are solving one more interesting problem, so a problem statement is I write a program.
To reverse a number, so we have to write a program to reverse a number, so W8 being WRP means vapid
means, right?
Program,
for example, of the given number is, let's say when 25, some output should be 521.
If the given number is today for some arbitrary forty three.
Similarly, if my input is, let's say five twenty one, some output should be one twenty five.
If they're in four days today to my output should be 23.
So we have to reverse.
No.
So how we can solve this problem?
So the logic is very simple.
So what we do is so what we do is, for example, let's say my value is my number is let's say 32.
So I will find last digit.
And more Ditton will give me the last digit, which is to.
OK.
And I will also maintain a number of laws, which is initially zero, and I will add to the rivers so
rivers will become too.
Now, I will update that number, no question number by 10.
So my new number is three.
Now I will again find the last digit.
So three more then it was three.
And now what I will do, I will write reverse equals reverse plus last digit.
So if you will write this.
So this is wrong.
So in previous example where we have to find the sum of their digits.
So we have written something like a sum equals some plus last digit.
OK, so the idea is very similar to the last problem, but this idea will not work here because if you
reverse is also zero, sum was also zero.
We are again finding the last digit.
We are dividing the number by 10.
But if you if you will write to us, it calls it was blessed last digit.
It will find some.
What will happen?
It will become reverse.
It goes all the way live.
Reverse.
Blessed tree.
It will become five.
So what we should do, reverse it was reverse first multiply two with 10, then add three.
So this will give me twenty three.
So the defense's last example, I did some inkwells someplace, last blizzard here where they will do
a reverse equals reverse, multiply by 10 plus last digit.
This 10 is extra, this multiplication with ten is extra.
OK.
You can also add I did some equals, some multiplied 10 plus last digit.
Is there a difference between the last question and this question is only this multiplication by 10
factor?
So let me show you how it works.
So suppose the value of any is 32 initially made, it was really zero.
So find the last digit, which is two.
So last digit it close to.
So reverse it calls reverse multiply by 10 plus last digit.
So initially reverse is zero.
So zero multiply where ten will be zero and last digit is too.
So reverse is true.
Now, I will update the number.
So my number is three.
How?
Because number is divided by ten.
Now maybe versus two.
So it will be the last widget.
Last digit is three and reverse equals reverse.
Which is to multiply by ten.
Plus the last digit which is three.
So output is 23.
Now update the number.
Number equals number by ten.
So number will become zero.
And I will stop.
And this is my output in variable reverse.
So only this multiplication things extra because we cannot add normally.
OK, now let's take one more example.
Suppose the value of end is 123.
Initially reverse is zero.
Last digit is three.
OK, so the reverse.
It was all really of reverse multiply by ten plus last digit, which is three some outputs, three.
So a reverse history.
Now update the number.
Number will become twelve.
So the reverse is three.
And we're doing the last digit.
It will be two.
So a reverse equals all of reverse.
Multiply by ten.
Blessed last digit, which is two.
So thirty two.
Now update the number.
Number becomes one reverses today.
So what do I last Richard.
It will be one.
OK.
So reverse it because all the value of reverse.
We just had to multiply by ten plus last digit.
Which is one.
So reverses to 21.
Now ablated the number.
So number equals number by ten.
So one by ten is zero point one.
But in upon integer will be integer.
So number equals zero.
Zero means.
Stop.
And this is why as a 221 123.
So I think you've heard the logic.
So what we will do?
So initially, my reverse is a zero, right zero, because otherwise it will contain garbage, will
you?
So surveille number is greater than zero whilom, but is not to close to zero or whilom, but it is
a good thing zero.
They're both the same.
We have to find last digit.
So last digit equals no more than now.
We've got the last digit and reverse equals reverse and multiply it by 10 plus last digit.
And then we have to update the number, number, request number by ten.
And finally, our output will be Reavers.
So this is the code that we have to write.
Okay, so the logic is very simple.
In case of some head, it was some equals zero.
And I heard it was some it equals some plus last digit rest.
All things were the same.
And here I was having see out some.
And some so this you can compare just we have changed the very real name because in the previous problem
and we won't talk, I read some hatred on Caligula was suggested the name of the variable is changed.
The main differences, we are multiplying by ten now.
OK.
So this is the main difference.
So let us ride called.
So now it does name this file is reverse.
Off.
No, not CPV.
So now what I have to do, I will take a number and and put fundi, use it.
I am taking a variable in reverse, which is initially zero.
So while my number is greater than zero.
What I have to do.
I have to find the last digit first.
So end last night, it equals no more then.
So do you give me the last budget and reverse equals halo's multiplied by 10 plus last digit.
And then we have to update our number, so, no, it was number by then.
And finally, I will print.
He was Soucy out reverse.
OK, so this is all that we have to do.
Now, let us test our program.
So let's say the value of and is 123, so our parties to twenty one.
Let's say my input is, well, same output is twenty one.
So if the value of friend is, let's say, three, six, five, four, some output is four, five,
six, three.
Now, let us bastard.
Well, last time.
So let's hear my input is hindered.
So what should be my output, my output should be zero zero one.
But let's see what will be my output.
So my output is coming out to be one.
So if the number is a hundred, my output should be zero zero one, but it is coming out to be when?
How so?
Let us right in our program to see what went wrong.
So my number and is hindered initially.
Reverse is zero.
And so what will be my last digit?
Last digit will be zero.
So reverse it was reverse, multiply by 10 plus last digit, which is zero.
Now update the value and will become 10.
So the reverse is zero.
Last digit is again zero.
And reverse is a reverse.
Multiply it by 10 plus last digit.
So reverses again zero.
Now update the numbers.
A number becomes one.
Reverses.
Zero.
So what will be my last digit?
Which is one.
So a reverse equals reverse.
Multiply by ten plus the last digit.
So my reverses, when not update the number number will become zero.
I will stop and my output will be one.
So that is the reason.
When is my output.
Margaret.
So how we can solve this problem?
Any idea?
So we can solve this problem, but we will lose.
So first, let us come into doubt.
So this solution that we have commented, so this solution that we are committed at is absolutely fine.
No one will check gold 400 at least in your college or in your school.
No one is gonna check.
So this court is hundred percent correct.
But what extra you can do is.
You just find the last digit and just print the last digit.
So while limbered is good in zero, just print the last digit.
So she wrote last digit, which is no more than.
And number equals number by an.
Now, that is all.
As soon as you find the last digit, as soon as you find the last digit just printed.
So a guest star program led to the release of 123 so Tilda and doing find the last digit and Brent.
Let them my in four days.
Three, two, one, three, two, five, six, four.
So Margaret is four, six, five, two, three.
And now let's say my input is handed.
My input is handed.
So my output is zero zero one, which is correct.
So how it is working, let's say, for any quiz?
123.
So what did we do?
Find the last digit, which is three Braintree.
Normally we come to Brent.
Last digit.
So 22 printed upbraid the number normally come when find the last resort.
And Brent when number really becomes zero.
This is my answer.
When I leave, the value of Andy's is hundred branded the last digit zero upbraid the number normally
become 10.
Brent, the last digit update number.
Ben, the last digit update.
The number number becomes zero.
Stop.
This is my output for near-term output is coming out with zero zero one.
OK, so just we are printing the last digit and more, ten and more means the last digit.
OK, this is last digit, but there is a problem with this solution.
So what is the problem?
So, for example, what I want to do is after finding that it was I want to perform some operation on
the reverse.
So in this case, I am not starting that rehearsed number.
So how can I perform operation on the reverse of the number?
Here in this case, if you want to perform some operation, we can write, for example, reverse equals
reverse, plus anything that you want to do.
For example, add Dwil.
So here I can do I can add two L to the reverse of the number.
But here in this case.
I haven't installed the reverse of the number, so I cannot add it to L in this code.
OK, so if someone asks you, which is the correct approach.
So I Bovin is correct approach.
And this is not correct approach.
So this is correct.
The second approach is correct only when the question was just to print reverse of a number.
OK.
So if the question was so if the question was just print the reverse of no, just print reverse.
My question is just print reverse, then this cord is hundred percent correct.
But if the cautions sees, write a program to find the reverse of the number and add 10 to the number
and then print reserved.
OK.
If caution is something like this, find the reverse of the number and attend to the number and then
print the result.
So this is much a better approach because here we are having the number stored in my variable reverse.
So we can add what we can perform any operation or net them.
But so if in university exam, anyone ask you to find the reverse of the number, the first approach
is correct.
Second approach is wrong.
OK.
So teacher will give you marks only when you write this call.
They will not give your marks for this call.
So that means if I had the first call for any quiz, hundred more bodies.
Again, when so can resolve this problem.
Can we solve this problem?
What I'm saying.
I am telling you to follow this approach, word for any was handed, it is giving me one as output when
asked to correct outproduce zero zero one.
So can we solve this problem?
So the answer is yes, we can solve this problem.
Myrt, with the current knowledge that we have, we cannot solve this problem.
We have to study Eddi for solving this problem.
OK.
So with the help of very this best case, finanical sended, this can be sold.
When we study, Eddie.
So after starting the Eddie, we really mortified discord and discord will work for an equals hundred
as well.
OK.
So there is one more interesting thing that I want to tell you.
So remember I told you something like I told you did in beta is of four bytes and what is my limit quoted?
About 30 went to minus one to minus two to the power to do.
And this was the range of the integer.
And I don't do remember that it is ten to the power.
Nine digits.
You can remember.
Okay, so this number I also told you that this number has a name intermix.
So what will happen if you reverse.
If you reverse intermix?
What will happen.
So there will be integer overflow.
Let me show you.
Just remove it.
Let's see the value of it.
And is intermix.
And Dumex me is true, the boat do and minus one.
So the real friend is intermix.
And let's say for the time being, Commander Daoud.
And let us try to bring the valley off.
And so she yelled.
And now let's see what is the value of intermix.
That makes in a murder can we can stored in India.
So this is the maximum number undertaken store in India.
OK, so two one four seven four eight three, six, four and seven.
So this is endemics.
Or against a two to the power tactic, one minus one.
So this is the maximum number.
This is the maximum number of dirt I can store in four bytes or a 32 bids.
So what will happen if you try to reverse this number?
So what is the reverse of this number seven forward six three eight four seven forward one two.
So this is the reverse of this number.
Now, tell me.
Let's say this is an Atlanta decision was.
So have you observed one thing?
Reverse is greater than the number.
This is a starting from two.
And this is starting from seven.
And the number of digits, I seem.
So a starting point here is to and starting point here is seven and the number of digits in both and
and reverse are seem so reverse is good then.
And that we can see that we can that we are observing.
But I told you that the maximum number that we can store is this number.
So how we can store this number in four bytes.
How we can store this number in four bytes.
So the answer is we cannot store.
OK, so what will happen?
So this is intermix.
So as soon as you will try you, as soon as you will find a number greater than intermix.
So I told you, wrap around so intermixed plus one, we will reach into a minimum.
OK.
So as soon as you will cross the valley of Intermix, you will reach into minimum.
So this number will not get benter in Strood.
A negative number.
A negative number will be Hmoud put out a negative number.
Will be my output.
Why?
Because I wrap around.
We take place and via it happened will take place because the reverse is greater than the number.
And this number is endemics.
So the reverse is greater than intermix.
Hence it will reach into minimum minus two to the power 31.
Minus one.
OK.
So negative number value output.
This is this thing clear.
So we call this thing as in digit overflow.
In deja overflow.
This thing is called and dijo and flow.
So let me show you.
Now, let us find the reverse of intermix.
Noller does test our program.
So you can see here, this was my number and this is the reverse of the above number.
And it is negative via negative because in digital of law is there wrap around is taking place.
OK.
So I hope you got the logic, but don't worry, these kinds of things will never be asked in college
or high school, high school exams.
But these things may be axed in interviews of top tech companies, for example, Google, Facebook,
Amazon, Microsoft.
OK.
So you have to take care of these things also.
I am not covering how to handle this case.
I will cover it later.
But you should know when winning the general will take place.
OK.
So just open your mind.
Think of all the possibilities that can happen.
OK, guys.
So this is it for this video.
Thank you.