Hello, everyone, welcome to this video.
So in this session, what we do, we will solve one more problem with the help of recursion.
OK, so the name of the problem is called digits.
OK, so what is the problem?
The problem is very simple.
Suppose you will be given a number.
Let's say the number is four zero two five and you have to tell the number of digits in the numbers
of the jerseys for.
OK, similarly, the number is 125.
Number of digits is three.
If the number is 20, the number of digits is two.
OK, so you have to count the number of digits representing the number.
OK, now what have to do so.
I have to write a function count, but it will take it will take a number as input and you have to tell
how many digits are present in this number.
And now nowadays a constraint on the value of N so the end will be greater than or equal to one.
OK, so the value often then what does N and will be a natural number.
OK, so this is our constraint and is a natural number.
OK, that means and cannot be zero and and basically cannot be less than or equal to zero and will always
be a positive number.
OK, so ok, so we have to calculate the number of digits present in Adam but now going to calculate
the number of digits.
OK, so what we will do, we will take the help of recursion.
OK, if you remember we have already solved this problem with the help of four Lopevi Lubert here since
we are using recursion, since we are learning recursion.
So we'll try to solve this problem with the help of recursion.
Now how we can solve this problem with the help of recursion.
So what will happen?
OK, so this is our number four zero to five.
So what I am planning to do here is what I will do.
I will spread this number.
OK, I will tell that equation to solve this problem for me, so the question Winterlude occasionally
turned me down, said three, and then what I will do, I will add plus one.
So I will be for.
OK, so what I will do, I will break my number and then I will tell Grigoryan, then I will tell you
to solve this problem.
So the question will give me three and I will add plus one.
OK, let's take one more example.
Suppose let's take a bigger example.
So let's say one, two, three, four and five and six, so how many digits are there in this number?
So there are six digits.
OK, what I will do, I will break this number.
And then I will tell Dacogen to solve this smaller problem for me, so the equation will return me five
and then I will add plus one so that output will become six.
OK, let's try on one example.
So suppose let's say our example is four zero two five.
OK, so the value of this, Gordana, goes to one.
So this number qualifies for our problem.
OK, so what I'm planning to do here is I will break this number at this part, OK?
And I will tell you to solve this problem for me, OK?
So I will reach four zero to number and then again, recursion.
The problem I will call on this part.
So I will call on 40.
Again, we will break and we will call on this part, so I will call on for.
Again, I will break at this part and I will call here.
So this is zero basically, so I am calling one zero.
So this will be our best case.
OK, this will be our best case, so what it will be done, it will return zero.
Now, our formula was this part of the sport, and I will add plus one.
OK, so zero plus one.
So I will return one here.
OK, so the onset of this part is one, and I will add plus one, so I will return to.
OK, so, Don, sort of this part of this part is to so and I will add plus one, so I will read entry.
So the onset of this part the has told me that the onset of this part is three and three plus one.
So our output will be four, which is our correct output, which is the correct output.
OK.
So if this isn't this whole reason, so for zero to five, if this is and now what is four zero two?
So for 02, this is inviting.
OK, so integer upon integer will be an integer, so four zero two five four two four zero two five
divided by 10 will not be four zero two point five.
It will be four zero two.
Because we remember integer upon integer will be integer.
OK, digital divide won't be there will be integer.
OK, so I will write a function count.
So this column function will take and as input what we have to do, we have to break our problem.
So what this current function do is it will take an integer and it will tell you that number of digits
present in.
And so what I will do, I will give, I will call the function on.
And by then so count will function, will give me how many digits are present in the number.
And then basically if the value of it is four zero two five, then what I'm calling I'm calling on four
zero two.
So the question will tell me the number of digits current function will tell me the number of digits
present in this is three I would have to do I have to add plus one.
So if I will add plus one, I will get the problem.
I will get the solution of the bigger problem rediscount often.
OK, so this will be our recursive case.
OK, and our best guess is if the value of any zero, I have to return zero.
OK, now one thing that you may get confused is if the value of any zero, why we are not returning
when we are not returning one here.
Because initially I told you that the value of Fennville bigger than our to one.
So this is basically invalid input.
OK, this will be invalid.
That's why I'm returning zero here.
OK, so let's write the code and then we will draw this diagram again, so what they're done by Britain,
they will be integer, OK, because I've got it done.
How many digits are present?
So let's write a function count.
It will take a number and and we have to find how many digits are present in the number and.
So best case will be.
So this is our base case, and this case is very simple, if the value of any zero.
So how many digits are present?
Zero digits at present?
OK, because this is invalid input then.
Again, recursive case.
So recursive case, small output.
Small answer, so small answer is if you want to calculate.
Number of digits and then calculate the number of digits and then Martin, OK, and then the calculation
part.
So calculation part is very simple, after calculating this moylan's that I have to add plus one and
done and let's test our function.
So I'm calling this function count.
For let's see.
Seven, eight, two zero.
OK, I'm calling this function for seven eight two zero.
Now, let's run our file so our picture before there are four digits.
OK.
So I would protest for several quarters, working fine now after getting the code with the help of BMI,
but what we will do, we will try to do that.
And our goal basically, we will try to draw the diagram so that we can have a better understanding.
OK.
So seven to zero, the value of seven, eight to zero, so seven, eight to zero.
OK, so this is not close to zero, seven to zero is not close to zero, so line number 10, I will
call on and by Martin, so I will call on seven eight to.
Then again, I will come at this line.
So basically 72 zero is waiting in line number 10, 72 is waiting out line number 10, then I will call
on 78 again.
I will call on this function.
So it will be a red line number.
Then it will call on seven.
Seven is not close to zero.
This also very hard line number 10.
So it will call on zero.
OK, and by then so seven by ten will be zero.
So if the value of any zero, I'm returning zero, so I will return zero.
So seven was waiting at line number 10.
So after the number 10.
Smolan Plus one.
OK, so the simple answer was zero.
So what seven will done.
Small answered plus one so it will return one.
Now here the value of small answer is one again.
I have to return Small said plus one.
So I will return to.
So the value of small answer is to save the residents moralizer plus one, so and three, then I have
to return small Lanser plus one.
So I had a of small answer is three.
So I have to return three plus one, which is four.
OK, so four is our answer for seven to zero.
OK, so I have seven to zero then I big this number I, I call the recursion on this part of the equation.
So this is 72 I big this number recursion.
I call the recursion on this part.
So this is seven.
Did I break this number.
Recursion will call on this part.
I break this, no occasion will call on this part, so it will be zero zero zero zero from here and
zero plus one, so I will return one one plus one.
So I will return to two plus one.
I will return three three plus one.
I will return four.
OK, so in short, you can understand it like this way.
I want to calculate the number of digits present in this part.
Besant in the numbers seven to zero.
I will break the number because it is big, the number big the bigger problem and a smaller problem.
And I am calling it a collision on this part.
So the equation will tell me you do not.
We have to assume, OK, I told you we have to assume so we have to assume that the equation will tell
me the answer of this part is three.
OK, now you don't have to think how the answer is.
Three, assume that the equation will give me the answer.
Three, so the question gives me the answer three.
And then I will add plus one.
So three plus one and the answer is four.
OK, so I'm assuming here that and then going function will give me the value.
Give me the answer for this part, so the answer for this, but will come out to be three and then I
am returning three plus one, which is for OK, so I hope you understood the gold.
OK, the problem was very, very simple.
OK, thank you.