Hello everyone.
Welcome to this video.
So in this session what we do we will solve one more problem with the help of occasion.
OK so the name of the problem is count digits.
Okay so what is the problem.
The problem is very simple.
Suppose you will be given a number let's say the number is 4 0 2 5 and you have to tell the number of
digits in the numbers so number of digits is food.
Okay.
Somalia.
The number is 125.
Number of digits is three.
If the number is 20 the number of digits is 2.
Okay.
So you have to count the number of digits present in the number.
Okay.
Now what I have to do.
So I have to write the function count what 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 there is a constraint on the value of.
So the end will be good then articles 2 1.
Okay.
So the value of in the end what is then and will be a natural number.
Okay.
So this is our constraint and is a natural number.
Okay.
That means and cannot be zero and and basically cannot be left not of course to zero and will always
be a positive number.
Okay.
So okay.
So we have to calculate the number of digits present in item but now how going to calculate the number
of digits.
Okay.
So what we will do we will take the help of recursion.
Okay.
If you remember we have already solved this problem with the help of four loop by little but here since
we're using regression things we are learning recursion.
So we will try to solve this problem with the help of recursion.
Now how we can solve this problem with the help of the occasion.
So what will happen.
Okay so this is our number 4 0 2 5.
So what I'm planning to do here is what I will do I will split this number okay.
I will tell the recursion to solve this problem for me.
So recursion what would load it occasion will turn me down.
I said three and then 12 what I will do I will add plus 1.
So output will be for okay.
So what I will do I will break my number.
And then I will tell regression.
Then I will tell revision to solve this problem.
So the question will give me tree and I will add plus 1.
Okay let's take one more example so suppose let's take a bigger example so let's say 1 2 3 4 and 5 and
6.
So how many digits are there in this number.
So there are six digits.
Okay.
What I will do I will break this number and then I will tell the equation to solve this smaller problem
for me.
So the occasion will return me 5 and then I will add plus 1.
So it output will become 6.
Okay let's try it in on one example.
So suppose let's say our example is 4 0 2 5.
Okay.
So the value of end is good then goes to 1.
So this number qualifies for our problem.
Okay so what I'm planning to do here is I will break this number at this part okay.
And I will tell recursion to solve this problem for me.
Okay.
So I will reach for zero to number and then again recursion big 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 1 0.
So this will be our base case okay.
This will be our base case.
So what it will done.
It will return 0 now.
Our form levels this part because it will give me the answer of this part and I will add plus 1.
Okay so 0 plus 1.
So I will return one here okay.
So the onset of this part is 1 and I will add plus 1.
So I will have done 2 Okay so does sort of this part of the equation told me downs of this part is too
so and I will add plus one so I will done three.
So that onset of this part discussion has told me the downside of this part is three and three plus
one.
So our output will be full which is our correct output which is the correct output.
Okay.
So if this is n this whole number is n so 4 0 2 5.
If this is n now what is 4 0 2 so 4 0 2.
This is and by then.
Okay.
So in Deja upon Deja will we end in Deja.
So 4 0 2 5 4 so 4 0 2 5 4 divided by 10 will not be 4 0 2.5.
It will be for it'll do we.
Because we remember it digit upon integer will we in danger.
Okay.
Digital divide won't be there will be in okay.
So I will write a function count.
So this current 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 n and it will tell me the number of digits
present in n.
So what I will do I will give.
I will call the function on end by 10.
So count will count.
Function will give me how many digits are present in that number and by then basically leave the value
of end is 4 0 2 5.
Then what I'm calling I'm calling 1 4 0 2.
So the equation will tell me that the number of digits column function will tell me the number of digits
present in this is 3.
Now what I have to do I have to add plus 1.
So if I will add plus 1 I will get the problem.
I will get the solution of the bigger problem let's count often.
Okay so this will be our recursive case okay.
And that will be a success if the value of any 0.
I have to return 0.
Okay now one thing that you may can get confused is if the value find the 0 0 5 you are not returning
when we are in order.
Don't need one here because initially I took a look at the value a friend will be good then order goes
to 1.
So this is basically invalid input.
Okay this will be invalid.
That's why I'm returning 0 here okay.
So let's write the code and then we will draw this diagram again.
So what do we that are done by Britain.
I will be in danger okay because I have got it done.
How many did your child present.
So let's write the function count it will take a number n and we have to find how many digits out peasant
in the number and so base case will be so this is our base case and base case is very simple.
If the value of any 0.
So how many did you decide peasant 0 digits at present.
Okay because this is invalid input then again recursive case
so recursive case small output small answer so a small answer is if you want to calculate number of
digits in n then calculate the number of digits and then by 10.
Okay.
And then the calculation part
so conclusion part is very simple.
After calculating this more long said I have to add plus one and they're done.
And let's test our function.
So I'm calling this function count for let's say 7 8 2 0.
Okay I'm calling this function 4 7 2 0.
Now let's run our file so our output should be for data four digits.
Okay
so I would put this forward civil code is working fine now after writing the code with the help of PMI
with 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.
Okay so 7 8 2 0 the value of land is 7 8 2 0 so 7 8 2 0 okay.
So this is not the case 2 0 7 0 2 0 is not close to zero.
So line number 10.
I will call an end by 10.
So I will call on 7 8 2 then again I will come out this line.
So basically 7 8 2 0 is waiting at line number 10.
Senator 2 is waiting at line number 10.
Then I will call in 7.
Again I will call on this function.
So it will wait at line number 10.
It will call on 7 7 is not to question 0.
This also will headline number 10.
So it will call long 0 okay and by 10 so 7 by 10 will be 0.
So if the value of any 0 I returning 0.
So I will return 0.
So 7 was waiting at line number 10.
Salvador the real number Densmore lines up plus 1.
Okay so the value of small arms that was 0 so what 7 Miller done small Lancer plus 1.
So it will return 1.
Now here the value of small lines that is 1 again I have done smaller small said plus 1 so I will return
to the value of small lines.
That is to say I have the right and small answer plus 1 so I will return 3.
Then I have to write down small lines that plus 1.
So I hate the value of small lines that is 3 so I have to attend 3 plus 1 which is 4.
Okay so 4 is our answer 4 7 8 2 0.
Okay so I have 70 to 0.
Then I beg this number I.
I call that equation on this part of the equation.
So this is 7 8 2.
I big this number.
Recursion I call the equation on this part.
So this is 78.
I big this number.
The equation will call on this part I break this number recursion will call on this part so it will
be 0 so 0 will return 0 from it.
And 0 plus 1.
So I will return when one plus one.
So I will return to 2 plus 1.
I will return 3 3 plus 1.
I will return food.
Okay so in short you can understand it like this way.
I want to calculate the number of digits present in this part present in the numbers 7 8 2 0.
I will break the number because the questions is vague the number will big the bigger problem and the
smaller problem and I am calling recursion on this part.
So the equation will tell me you do not we have to assume okay.
I told you we have to assume so we have to assume that the equation will tell me the answer of this
party's tree.
Okay now you do not have to think well how does that tree assume that the recursion will give me the
answer tree.
So recursion gives me the answer tree and then I will add plus 1 so 3 plus 1 and the answer is four.
Okay so I am assuming here that and by then common function will give me the value.
Give me the answer for this part.
So the answer for this one will come out to be tree and then I am returning 3 plus 1 which is 4.
Okay so I hope you understood the code.
Okay.
The problem was very very simple.
Okay thank you.