 Hello friends, I am Sanjay Gupta, I welcome you on Sanjay Gupta Tech School. In this video I am going to explain how we can write algorithms. So if you watched my previous video that was also related to how we can write algorithms. So in that video I explained you the way through which we can write algorithms but in this video I will be telling you another way also. So a problem can be implemented in two different ways that looks like an algorithm. So in this video you will learn another way as well. So in my previous video I explained you how to write sequential algorithms. So right now I am taking an example of swapping. So at the end of the video I gave you five problems. So from those five problems this is one of the problems that I am going to solve. So let me write the first way or method of implementing an algorithm. So here step one begin then step number two read values of A and B then step number three. So from step number three four and five I am going to write the logic for swapping. So here set C equals to A then step number four will be set A equals to B and step number five will be set B equals to C. So these are simple swapping steps that you might have learned in C language as well. So after this swapping operation I can write print values of A and B. So after swapping whatever values are available in A and B variables will be printed and then I can end this algorithm. So this is first way that I also explained in my previous video. Now in this video you will learn another way of writing this algorithm. So here you can see input is provided by user then process is applied and output is displayed. Now in real time scenarios whenever we will be implementing some problems through our programming language so there will be various functions available. So this algorithm can be implemented or similar to a function loop where input will be not received from the user. It will be passed by another function. So you can write it like this. So you need to assume that the input values will be available into these variables and after this header you need to write the steps. So here you can directly write set C equals to A. So now you can compare it. So in this way we use step number as begin and step number two as read values of A and B. Those are not available here. So you can imagine it is a part of a complete process and name of this process is swap and it is receiving two values as input. So don't write read statement here because A and B are already having values. Then step number two will be set equals to B. Step number three will be set B equals to C. So these are simple statement statements. Now in step number four I can write then values of A and B and then we can add to this algorithm. So these are two different ways through which you can implement any algorithm. So first method is very simple and second method is similar to a user defined function. So if you are going to implement this algorithm in C programming. So you need to implement swap functions separately and you need to implement main functions separately and main function will be calling the swap function and passing the parameters. And if we talk about this, so here you don't need to implement any separate function. All steps can be implemented through main function. So in my upcoming videos those will be related to array, lake list and any other algorithm. So you will see this header always because that process can be called by any other method. So that's why I will be using these approach in those people. So this was first way. Sorry, this was first type. So now I'm going to explain second type. So second type was business making algorithms. And the process will be even or odd. So this is the problem for which we need to implement the algorithm. So again, I'm going to implement two different ways. First will be having all the steps from begin to end and second will look like a sub method. So I'm going to implement two different ways. First will be having all the steps from begin to end and second will look like a sub method. So here, step number one is begin. Then step number two, read value of end. So here you can see we need to check even or odd. So we need only one number. So I'm going to read that number into end variable. Then here I am applying if so and more to equals to zero. Right. So I'm writing more here because I can't use modulus operator because that might be available in any programming like this or any other simple will be so I'm just writing what so and more to if it's remainder will be equals to zero. Then I can write print. And inside double quotes, I'm writing even. Then else. I'm writing print. And also you can write and off it. So this is optional. If you want to write and off it, then you can use it. Otherwise, you can also move it and step number four will be. So this is these are simple steps. Starting from begin to end. Now how we can write it like so that it will be having a function like look. So you can write even or as its name and then here you can write step number one. So now you don't need to write the statement the value of end. You can directly use these steps here. You can directly use these steps here. So now I'm writing if. And more to equals to zero. So I can write print. Inside double quotes. I'm writing even. Then as print or. Then end of it. And step number two will be. And so this way you can write function like. So I hope you understood because both are very simple. Only changes. We are not reading values from user in this. We are. We are assuming that a value will be available into this and variable because this sub process will be called by another process. By implementing in this way. So this was a second type of algorithm. That is decision making. Now I'm coming on to third time. Her type is iterative algorithm. I'm removing. I'm raising these. So her type is iterative algorithm. And our problem is. Calculate. So first I'm going to implement this easy way. And then I'm going to implement. The function like way for this problem. Right. So. Step number one. Begin. Then step number two. Leave. Value of an again, we need only one input because we need to calculate factorial of a particular number. So that will be available in. And now in third step, I am writing set. I equals to one. So this will be loop counter, which is starting from one. And I'm writing F equals to one. So this will be the variable in which factorial will be stored. So. I and F both are initialized with one. After this, I'm writing. Repeat. So it is responsible for repeating some set of. So repeat. Steps. So here I'm going to write. Which steps I'm going to repeat. And. Why. I. Less than equals to. And so this is the condition. Right. So you can. You can write. Step number. So. So. So. So. So. So. So. So. So. So you can. You can write. Step numbers here after completion of these steps. Because you don't know how many steps will be. With this step. So leave it blank and after writing, you can put steps. So. So. Step number five will be. As. Equals to. And. Step number six will be. So here you can also write. I. Equals to. I plus one. Right. So. These are the steps. Then. End of. So this is also optional. This indicates that. You will be starting from a whole step. And it is ending after. Six steps. So. Now you can write that number five and six. So. You can read this repeat. Steps. So. So. So. So. So. So the by. Five. Is. One. Equals. Right. So this way we implemented this algorithm now after completion of this. At. Step number seven. I can write print. Value of. F which is containing the. And. Step number eight will be. End. So this way I implemented. The. to first method. Now we can convert this algorithm into function like algorithm, right? So what I need to do, I need to remove these from the top. So I'm not going to need input from the user. I'm assuming that this method will be called by another method and that method will be providing the value. So here I'm writing factorial and inside bracket I'm writing n. And this will be step number 2, step number 1, 2, 3, step number 4, 5 and 6. And we are going to repeat step number 3 and step number 4. So this way, this is the algorithm and this is the platform that we are going to use to understand array, link list, stat, queues. Those are available in data structures. So I hope you understood all the types of algorithm writing now. So first was sequence algorithm. Second was design making and third is iterative algorithm. And I explain you all three types or type of algorithm with two different methods. One is having input process and output on the steps. And another one is similar to a sub method which don't receive input from user, which receives input from the method which is calling that sub algorithm or method, right? And overall process will be same in both methods. So remember, we are going to use this second method that we discussed in this video in future to understand various concepts of data structure and algorithm. So I hope you understood whatever I explained in this video. So keep watching videos related to data structure so that you can prepare the subject for your study. Thank you for watching this video.