Hello everyone.
Welcome to this recession.
So in today's session what we will do we will try to solve one more problem with the help of the occasion.
So the name of the problem is sum of digits.
Okay then maybe the problem is some of the digits.
So what is the problem.
So the problem is very simple given the value often let's say it is one two three four and five.
What we have to do we have to add all these numbers so the addition will be 15.
So this is our answer.
Okay.
So we have to add all the digits of the number.
Okay.
Similarly the number and is 13 and I said will be three plus 1 food.
If the value of an is doing now there will be two plus one which is three.
Okay.
So I hope you got done said of the value of this five.
And the answer will be five.
If the value of and is zeros then the answer will be zero.
Okay.
So what we have to do we have to add all the digits of the number.
Okay.
And then we have to return the sum.
Okay.
So we have already solved this problem might actively using Before we divide loop.
But now in today's session since we are learning regression we will try to solve this same problem with
the help of the occasion.
Okay so what triggers Genesis occasions is I will if I really can get down some with a similar problem
and you do the calculation.
So what it goes it will do.
So we have to find some of digits of any number some digit of the anything number.
Okay.
So what recursion were ludicrous you will see.
So what they will do I will break the problem into smaller part.
Okay I will qualification on this part and occasion will give me the answer.
So what do the answer.
Because it will give me that dance have all the smaller problems then.
So you know the smaller answer how you will conclude the bigger answer.
What I will do smaller answer.
Plus the last digit.
Okay so this is last digit.
So smaller surplus last digit will be our answer.
So 10 plus 5 which will be 15.
Okay.
Similarly now how it will work internally.
So.
So this is our number okay.
Let's make the whole code red.
So what will happen.
I will break the number the smaller part I will call the equation on this part then this is our number
1 2 3 4 I will call that equation on a smaller product again I will call the equation on the smaller
part again I will call the equation on smaller part and again I will call the equation on the smaller
part.
So this is 0 so I am calling records on this my love part.
Okay so I am calling guys on a similar path so it will be zero.
Okay.
And the value of and is zero this is the smallest problem so this will become our base case so it will
return 0 okay.
So Don some of the smaller problem is 0.
And then we have to add the last digit so 0 plus 1.
It will be 1.
Now the answer with the smaller part is 1.
We have to add the last vision which is 2.
So one plus two is three.
Now the answer for the smaller part is 3.
Now we have to where the last digit.
So two plus three is six.
Now the answer for the smaller parties came out to be six.
Now we have to add the last digit.
So six plus four which is 10.
Now the answer for the smaller part came out to be 10.
Now we have to add the lassitude to calculate the bigger problem.
So 10 plus 5.
It will be 15.
Okay.
So I hope by it now do we know the formula.
So if you want to calculate some off and what we have to do.
So some will be a function.
It will take an M but n as input and it will give me the sum of the digits.
Okay.
What I will do I will give the sum function.
The smaller problem.
So this value is and by 10.
Okay.
So this will be what I will do.
I will call.
I will call the same function sum and I will give smaller problem.
I will give it number by 10.
Small problem.
So I am giving this as input.
So some will give me the output for the small problem.
And now what we have to do we have to add the last digit.
Okay.
So I will add the last digit okay.
So I am adding the last digit
okay.
I am writing the last digit.
Now let us write the code and then we will do.
Then they will write in again and we will again make the diagram.
Okay.
So first of all the return day will be in digit.
We have to return the sum.
Let's say the name of the function is someone Lee and what it does.
It will take a number as input and now the base case.
So base case is very simple.
The smallest problem whose solution we know.
Okay.
So the value of any zero obviously the sum will be zero.
Okay so this is our base case Okay so this is base case.
Now it's time for the recursive case.
OK so let's write a recursive case so for the recursive case what will be our small answer so our small
answer will be very simple.
So small answer will be calculate the answer for end by then.
Okay.
And now I want a conclusion.
But so far the calculation but what we have to do we have to find out the last digit first
okay.
So for calculation what we have to do what is our last digit
so calculating the last digit I can do something like this and more tonight.
So with the help of this court I will get the last digit.
And then what I have to do I have to return small answer plus last digit okay.
Now let us call this function let us call the sum function and let us give the value 1 2 3 4 5 as input.
Okay so what we write and set our answer will be 15 okay.
So let's that this file so our answer is coming out 215 our code is working fine.
Now let's try to make the diagram again and see what's happening okay.
So the value of and is one two three four five.
And so this is our value one two three four and five okay.
So this is not close to zero.
So I will come at this line and I will call four and by 10.
So I I will call it 1 2 3 and 4 and this function is working a line number eleven.
This function will go somewhere that line number eleven because it will call 1 2 3 it will wait at line
number eleven.
It will call well it will wait at line number eleven.
It will call 1 and it will wait at line number eleven.
It will call 0.
So what will happen.
It will are done zero.
So it is returning 0.
Okay.
So the small answer small answer is zero then I am calculating the last digit which is and more than
the value of this one.
So one by ten that will be when only.
Okay.
So small answer plus last digit.
So 0 plus 1 which is 1.
So I am returning 1 Okay.
Now here the value of small answer is 1.
Okay so last digit and more 10 so well more than that will be 2.
Okay so the last digit is 2.
And then I am returning the addition of these two so two plus 1 3 I'm returning 3 now the value of small
answer is 3 and then I'm calculating the last digit.
So lost digit is one the.
MARTIN So last digit is came out to be three and then nanometer Dunning the addition of these two so
three plus three which is six so I am returning six small answer is six.
And last digit will we.
So and more ten so that is it is this this will be our last digit.
So last as it is four so six plus four.
I'm returning the addition of these two so I am returning 10 and small answer is 10.
And the last digit will be and more 10.
So last digit will be five and then I am returning the addition of these two so 10 plus five which is
15.
Okay.
So that's how my answer is give out to be 15.
Okay.
So I hope you have understood the code.
So the code is very simple.
We have to write the base case then the recursive case we have to calculate.
We have to call the same function for a smaller input.
Okay.
And then the calculation part.
Okay.
So we are writing the code with the help of PMI only.
Okay.
We are thinking about this diagram late that first we are thinking about this diagram later.
First we are thinking only in terms of PMI we have to think of three steps.
Base case because if case and the calculation part okay if you will write these three steps perfectly
then linear call will definitely work.
Okay.
So I hope you understood this problem.
Thank you.