Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, so now what do we have to do is to write the recursive function that we'll be able to find
out the sum of all digits in a given number.
And we know that he can be easily done by using some iterative approach.
You simply take a number, let's say two, one, three.
And you know how to sum all of its digits by simply every time taking the right digit.
For example, three.
By using a model.
Right.
And model loop.
And you take these three and you submit to these one.
And then you take these two and you sum everything up and you get your six.
At the end.
OK.
So that will be an Darah iterative approach where you take this number.
Take the model divided by 10.
Take the right digit.
And then divide the whole number by 10.
And you just remain in the second phase with 21.
Then you stay with two.
And every time you take the right digit and you are basically OK.
So now in the recursive approach, we are going to use something very similar.
OK.
So we know that to find out on the sum of digits, let's call it a sum of digits, some of digits of,
let's say two, one, three.
It is simply it.
That's our main problem.
OK.
And we are looking because we are using recursion.
We are looking to find some relation to a sub problem.
So we know that some of digits, four, two, one, three equals two, three.
OK.
Plus some of digits, some of digits for 21.
And the reason for that is very simple.
We are going to take off the right digit.
Win this case.
It's three.
We know that it's three plus some digits for it.
Two and one.
And remove this digit because we also are already took it into our considerations.
We will remove it and call this function in a recursive manner.
And we know that this problem, this sub problem, can be divided even further.
So we will keep these three.
So it's three plus some of digits.
Four twenty one will be equal to one.
Right.
The right digit plus some of these.
Just some of digits for two because two is what remains if we divide twenty one by ten.
And we know that ah that's kind of a trivial solution because some of digits when you have just one
digit is just the digit itself, whether it's one, two, three, four or five or whatever, it will
be the digit itself.
So that will be used for our stopping condition, for our base case.
And we can see that we were able to take a bigger problem, a bigger problem, and to find some rules.
Some are regular things that we can do to divide it into sub problems.
So we just extract, extract the right digit and then we call these function recursive call of.
These given num divided by 10.
So two one two one three divided by ten is just two one.
OK, so two hundred and thirteen and we get twenty one.
So let's break.
Now let's see how we create a function for it.
And these function is going to be aimed some of some of digits.
And it will return some number.
OK.
And what we are going to do here is, first of all, we need to specify the stopping condition.
So in this example, we said that the stuffing condition, the base case is going to be when we have
just one digit.
And since we are working with integers, it's going to be very simple.
We just need to specify as long as if NUM is less or equal to nine.
OK, meaning it's less or equal to nine.
It can be nine, eight, seven.
It can be zero.
So if that's the case, we are going to return to return the number itself.
OK.
So it's trivial.
We can find the solution right away.
We don't need to make any division to solve problems and so on.
OK.
So what will be what will be the recursive call?
What will be the recursive call we've seen here in this example that we first of all extract the right
digit right.
We extract the right digit of a given number.
So that can be done by using a model of 10.
So return num modulo 10.
OK.
It's the right digit.
The right one.
Three in this case.
And one in this case.
Plus.
Plus what.
Last sum of digits.
Sum of digits.
Four for these twenty one which is.
Are these given number divided by ten.
Because we don't.
Want to include these three in the freezer?
Further south problem, because we already took it into account here, so it will look like it is numb,
divided by 10.
OK.
And basically, basically, we can say that that that's it.
So let's write some simple program to test it out.
So ain't no arm and train have enter insert to insert a number, then the user specifies the scarf while
I'm typing really fast today.
Now up.
Here's the mistake.
And now we are going to call these functions.
So print out.
We are going to print out right away.
We are not going to create another variable to hold the result.
We are going to create print.
There are some of digits for percentage.
They equals two percentage D..
OK.
And instead of these first place holder, we are going to use NARM, which is the number itself.
And here we are going to use some of digits.
OK.
Make these function call salved digits for now.
So instead of these percentage they will have Nahm so some of digits for a num for a given number.
Let's say two one three are will be equal to percentage Judy.
To these result.
That will be instead of these function ones.
It's on over with the execution.
So let's try to build and run it and see what happens if everything OK.
And we didn't mess up anything are in the process.
So OK, so here's the problem.
OK.
So that's just a comment.
Don't be so mad and now build and run and enter.
Insert the number.
It's go with this example.
And we know that we expect to receive six at the end of it.
Right.
Two plus one plus three.
So some of digits, four to one three equals two six.
Fantastic.
We can use some another example, let's say nine nine nine one zero five.
So we just sum it up.
It's 27, 28.
It's going to be thirty three.
Right.
So let's print.
Enter.
And there you go.
You can find your sum of digits for any positive number that you may insert it.
Of course, as long as it does not exceed the integer range.
So that's basically it for this video, for this challenge.
I hope you try this one on your own and you probably got it right because there is nothing special here.
We talked about how to find how to access digits in a given number in one or two or four previous challenges,
not in this section, but in the previous sections.
And there is nothing new here.
We just users use the base case.
We used some recursive call and we find found out some rules for the use on their vision to solve problems.
So thank you guys for watching.
I hope this material is clear to you.
And in the next video, in the next challenge, we are going to look for also something similar to some
of digits.
But let's go there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.