Hello everyone welcome to the session.
So in this session what we will do so we will try to solve one more problem with the help of the occasion.
So the name of the problem is printed numbers.
Okay so we will cause this problem.
We will solve this problem with the help of the equation.
So what this problem suggests.
So if the value of minus five.
What would I have to do.
So I have to print the numbers from 1 2 n so if the value of find his wife I will print the number 1
2 3 4 5.
If the value of and is three I will print the numbers one two three.
Okay so what I have to do I have to write a function print.
And and this print and function what it will do it will print the number.
It will start printing from 1 Bill.
Okay.
So this is the bulk of dysfunction.
We can solve this problem very easily using the for loop all the while loop but we have to solve this
problem using a regression.
So using regression what I have to do if I want to solve the problem for n then i have to solve the
problem for the smaller end.
Okay.
So what this print n function will do.
So this print n function it will print the number from 1 2.
Okay.
So this print then function it will print the numbers from 1 2 and so can I write like this.
So what this print and minus one function will do so print and function its job is to print the numbers
starting from one deal and what this print and minus one will do.
So it will start printing the number from one bill and minus one.
Okay.
And this is our goal and this this is and this will be the output of and minus one function.
Then after printing that and minus one we will print n.
Okay.
After printing and minus one we will print and that's done so.
And this time will also get printed.
So these two will become equal.
Okay.
So this will be our recursive case simple enough.
Okay.
So let's write the code for this.
So first of all you're too little return type return type will be void because I'm not I'm not returning
anything I'm basically printing.
Okay so I have to print what it will take it will take I integer s argument.
Okay.
So first of all what will be the base case what do with the base case so base case is the smallest smallest
problem whose solution we already know so the smallest problem will be if the value of any 0.
So if the value of an Israel would have to do what I have to print I do not have to print anything.
Okay.
If the value of an Israel do not print anything.
So this would be our base case because I have to start printing from 1.
You can see here we have to start printing from 1.
If the value of and is 0 we will not print anything.
Okay.
You have to start printing from 1.
You can also write the base case for any question also if the value of land is 1 then print 1.
Okay.
So as you wish you can as you wish you can modify the code.
Okay.
So the return type is void hence I am not understanding anything.
Okay.
So if the value of any 0 then do not print anything otherwise first what we have to do recursive case
so recursive case will be so this function print function it prints the number from 1 2.
And now I'm calling this print function for and minus 1.
What will happen our numbers will get printed from 1 to 1.
So what will output of this line this line will print the numbers starting from 1 bill and minus 1.
Okay so this is the this is our output a line number eleven.
Okay.
So this is our output to line number eleven.
Now our third thing.
So the calculation part what I have to do I have to print and it's dumb.
Okay.
Now let's print Okay so this is our output.
Okay so this print function what is it.
What it does is it prints the number from 1 2 in print and minus 1.
It will print the number from 1 to N minus 1 and then I am printing the anything but okay now let's
call this function let's call this print function for let's five.
Okay.
Now let's send in our file.
Okay so this function is not returning anything so I have to call this function.
I cannot write you out okay because this function audit ending anything.
Now let's send our file
so the -- that output 1 2 3 4 and 5.
Okay so our code is working fine now after writing the code what we have to do we have to make the diagram.
We have to brighten our code so we can understand it better.
Okay so print 5.
The value of an is 5.
Okay so this is 5.
So I am writing the output here.
I will write output here.
Okay so 5 will call on 4 4 will call on 3 3 will call on 2 2 will call on 1 1 will call on 0.
So if the value of any 0.
What I'm doing.
Return.
So I will come here.
We're done.
Okay so when was waiting at line number eleven when was waiting at line number eleven.
Now I will reach your line number 13 C out and okay so add at this line what will happen C out and so
when we'll get printed.
Now you can write a written statement here also.
Okay.
The return statement this is optional.
So what will happen.
I will come here okay.
So the value of this to so do was also waiting at line number eleven then CO2.
So two will get printed so one get printed first then 2 printed.
Then you're done.
I will write the line you're done.
I will write the statement then but this is optional.
Okay because the code ends at line number 15.
So I think Red done is optional here.
So the value of industry now tables get printed.
So write down the value of minus 4 4 will get printed.
The value of minus 5 5 will get printed and then find literate and I will reach the mean and then return
the other program will be all what.
And this will be our output.
Okay so you might.
So you might have confusion.
So I am returning.
I am writing return here.
Okay.
It will an artifact Delacorte.
I've got will still work fine.
So our output is still one two three four five.
Okay.
Java code is working fine.
Now one more thing so here I abandon them both in ascending order.
I am printing the numbers in descending order 1 2 and now what if I print I want to print the number
in descending order.
For example if the value of and is 5 I want to print 5 4 3 2 and when.
Okay this is how I want to print the number.
Basically I want to print the number in descending order.
Okay so what we will do let's say I am writing print to function so print to what it will take it will
take and as input and its work as to what it will do it will print the numbers in descending order five
four three two and one.
Okay so first of all what I will do I will print and okay.
Because the value of this 5 5 is getting printed first.
So first of all I will print that number and and then I will call the function and minus 1.
So what.
And minus 1 will do so and minus 1 will start bringing the numbers from and minus 1 then and minus 2
L1.
Okay.
And first of all and we'll get printed and our book was also the same.
We have to print the numbers in descending order.
Okay so first I have to print n and then I will call.
I will call the same function here.
It will be the opposite of this.
So first I was calling and minus 1 and then I was printing and but here first I will print n and then
I will call and minus 1.
Okay so basically defending or there will be the opposite of the ascending order.
Okay.
So what I will do.
Let's write print to function.
Let's copy discord
so this is print to function.
Now what I have to do I have to just change the order of these two lines.
So first I will print n
okay.
So this will be our print to function so print to function what it does is.
So this is our base case.
Now first I will print n and then I will call and minus 1.
So sorry this will be print too.
Okay print to function.
So what this function will do it will start printing the numbers from N minus 1 bill when.
Okay so this will be the output of this line.
But before the output of this line I am printing and OK now let's call print to function.
So I am calling pen to function and I'm giving five his input so our output will be five four three
two and one.
OK so let's write a novel called
so all our output is 5 4 2 2 and 1.
Now after writing the code what we have to do we have to do that and have a code so that we can understand
what is going on here.
Okay.
So the value of end is five.
Okay so this is five.
And I will write my output here.
Okay so 5 is not close to zero C out.
And so five will get printed and then I am calling print to function with the value in minus one.
Okay.
So the value of this food for is not too close to zero C out and so forth will get printed.
Then we are calling with a smaller function.
Okay I will call entre then t will get printed.
I will call on when I will call in to do is not close to zero then I will print to then to really call
on 1.
So 1 is not close to zero.
So I will print one then I will call on zero so zero equals zero then I have what I will do I will have
done so I will have done so when I was waiting at line number 26.
Okay this one was waiting at line number 26 then return.
So it is done.
Who was also waiting at line number 26.
And then you're done so I will are done.
He was waiting at Lam 9 number 26.
Then it will get done.
Ford was also waiting at number 26.
Then you're done.
And then five was also waiting at line number 26.
And then you're done so get it done so and this is our output five four three two one.
Okay.
So I hope you understand the code how it is working.
Okay.
So writing this down part writing this written statement is optional here.
If you want you can remove it also.
Okay.
For example let's remove this line and then also have a quota of accord will work because the function
ends here and if the function ended it automatically means that are done okay with the void.
You have option to return.
This is optional here but writing return here is mandatory.
Okay.
Writing the written statement inside the f is mandatory.
Okay so this done is compulsory otherwise what will happen if I will order write the return statement.
What will happen.
I will come at line number 22 which is wrong.
Okay.
So writing that are done is mandatory here.
But this is done is optional.
Okay.
And also this are done is optional.
Now I command out the certain part.
Now if we will run our program our program will still work fine.
Our output to live a full crew to anyone.
Okay so I hope you understand why this redundant mandatory I am led straight to so it is a value of
industry.
Let's say I am calling in to function what the value of industry.
So 3 what will happen 3 will call on 2 2 will call and 1.
Now one will call and zero so zero equals zero.
So if you will not write the return statement what will happen.
So zero will come here see out zero so zero will get.
This is our output.
So what will happen.
Zero will get painted Okay so this is brand to function.
So what will happen.
Our output is 3 to 1.
Now what will happen the value of n is zero.
So zero equals zero.
But if the return statement is not here then what will happen.
I will come at 9 number 23 so 0 1 get printed then I what I will do I will call on it to function and
minus 1.
So it will call on minus 1 then minus 1 will get printed it will equal and minus 2 it will equal and
minus 3 and so on.
Basically infinite loop problem will be there.
Okay so infinite loop problem will be there.
That's why this return is mandatory here.
If this are done is there what will happen.
I will return from zero.
Okay.
And this will not happen.
Okay so this return is mandatory and it is this written is optional.
Okay.
And similarly for this one also Fort Bend function.
Okay.
What will happen.
Suppose the value of and is 3.
Now what will happen and is not close to zero.
This this is our output.
I am calling one smaller part.
So 2 2 will call on 1 1 will call on 0.
Okay so zero equals zero.
So if the return statement is data what will happen.
I will come at 1 and then one will get printed.
But if this written statement is not there what will happen.
What will happen when I call on.
It also the requisite.
The written statement is not there.
Then I will come here at line number eleven.
I will call minus 1 then minus and will call on minus 2 minus 2 will call on minus 3 and so on.
Basically infinite loop problem will be there.
That's why return is mandatory here.
Okay.
Inside the base case returns treatment is mandatory.
Okay.
Remember this thing.
So I hope you understood the school.
Okay thank you.