 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how can write an algorithm. So this video is related to data structure and algorithm subject. So you might have this problem like how we can write simple algorithm while studying this subject. So today I will tell you three kind of algorithm like how we can write sequential algorithm, how we can write conditional algorithm and how we can write iterative algorithm, which will be implementing loops. So these will be very basic algorithm and in later videos, you will understand how we can write algorithm for arrays, linkless, tree and grass kind of things. So here you can see first point is there are no well defined standards for writing algorithms, rather it is problem and resource dependent. So there's no syntax for writing any algorithm as we have in programming languages. So we just need to focus on the problem like how we can write the steps so that this problem or this solution of the problem can be implemented via programming. So that solution of the problem or step by step procedure is known as algorithm. So you can write it in your own way, but it should be in certain steps so that it can be easily implemented by any programmer. Second point is algorithms are never written to support a particular programming code. So I think you understood this. So whenever writing any algorithm, you don't need to know any programming language, you can simply write the steps for the solution of a problem. And if you know the programming, then you can implement it. Otherwise, the person who knows programming that can implement those steps with that programming language. Then third point is as we know that all programming languages share basic code structure like loop. So in most of the programming languages we have do for and while loops, we have flow controls like if else, etc. So these features are available in almost all the programming languages. So these comments constructs can be used to write an algorithm. So while implementing the process, so there can be scenarios like do you want to do this thing or not. So they can be scenario like yes or no. So that can be done with the help of if else and there can be scenarios like we want to repeat these steps more than one time. So those can be implemented with the help of news. So these programming constructs we can also implement in algorithms so that we can reduce number of steps while writing algorithms. Then next point says the right algorithms in a step by step manner, but it is not always the case. Right. So next point is algorithm writing is a process and is executed after the problem domain is well defined. So if you understand the problem very well then only you will be able to write the algorithm in well way otherwise not. And last point is this is that is we should not know the problem domain for which we are designing our solution. Right. So if you don't know the problem then you will not be able to write the solution or so this is brief about some key points related to algorithm now moving to first type of algorithm that is sequential algorithm. So we will focus on certain things here. So you can read the problem statement which is written at the top. So let me take the highlighter. So here you can see the problem statement is designer algorithm to add two numbers and display the right. So input will be two numbers then we need to apply the addition process and then there's some will be a result. So in step one you can see we need to write this again. Then in step two you can see we are writing lead values of A and B. So we need to mention our variable names as well so that we can identify in which we are storing the values. But here you can see we are not mentioning the type of variable like values will be in these are flow character. We are not mentioning that we are just saying that lead values into A and B. Then in step three you can see addition process is available and for assigning purpose we are using colon equals to. So in algorithm we need to use equals to sign at two places first for assigning the values and second for comparison purpose. So in C language if you have gone through with C language so you might have double equals to sign for comparison. So in algorithm we don't use that double equals to sign because in some of the programming language we have triple equals to as well. So for comparison purpose we use single equals to in algorithm and for assignment purpose we use colon and equals to sign like here we use. So the addition of A and B will be assigned into C and then display value of C is available here and then add. So to read and print the values we just mentioned read and display we didn't use print up scanner we didn't use the out scene. So we are not focusing on a particular programming language we are just focusing on the process and the steps right so in your own language you can write the steps if you want to implement any algorithm. So I think you understood this now moving on to second one which is decision making algorithm which is related to if and else. So here you will see this single equals to sign. So here I am using this for comparison purpose here colon sign is missing just single equals to is available right and the problem statement is design an algorithm to compare two numbers and display them. So begin then read values of A and B then if A equals to B. So if both are equal then we need to print equal else we need to print not equal and then end. So here you can see these are the simple steps which are telling you like how many numbers you need to read then what condition you can apply what need to print when condition is true or when it is false. So this way if you convert this problem into a programming into any programming language like C or C++ then you need to decide like if you're using C language you need to use print up scanner for reading and printing. If you are going through C++ then you will be using C out and C in. So each programming language have their own reading printing statements or sentences. But here you can see in both the algorithms in this and in this we use lead and print common English words. So this is the way you will find writing of algorithm is very easy. You just need to focus on the solution of the problem. And this is the third part which is iterative algorithms. So problem statement is designer algorithm to print natural numbers up to end right. So we need to print all the natural numbers starting from one to end. So here you can see begin then read value of and so here we are going to read only one value because our problem require only one input from the user. So that's why and is mentioned here then set I equals to one. So here you can again focus colon and equals to are available. It means one will be assigned into I. And if we use only equals to sign then it means we are comparing two values. Then for repetition purpose we are using repeat. So repeat means we are applying a loop repeat steps while I less than equals to end. So this condition is true then some set of steps will be repeated. So you can also write number of steps like repeat steps five and six while I less than equals to end. So this way you can also write. So see there is no hard and fast rule while implementing or writing an algorithm. So you can modify writing of algorithm in your own way also because you are not implementing this algorithm as it is in any language. So you are going to convert this algorithm with the syntaxes. Those are available in any programming like this. So that's why you can use any changes while writing algorithms. Then here you can see display value of I and then I equals to I plus one. So this way value of I will be printed I will be incremented by one. Again this condition will be checked like while I less than equals to end. So this condition is true then step number five and six will be repeated. And when this condition will be false then loop will be and and algorithm will be stopped. So this way this is iterative algorithm. So I know these are very simple examples and in upcoming videos you will see some difficult examples as well. So with these simple simple examples, I hope you understood how we can write sequential or sequence algorithms. How we can write decision making algorithms. Those are dependent on certain conditions or maybe they are true or false. And how we can write loop dependent algorithm so that we can repeat some set of some set of steps with the help of repeat keyword that that is used here. Right. So these are some practice book related scenarios. So I'm just just explaining like how you can use these algorithms and which algorithm will be suitable for which question. So if you talk about first question that is write an algorithm for swapping of two values. So you can use sequential algorithm for this because there is no if else required and no loop is required for a problem statement to write an algorithm to find the large of two numbers. So here you can use decision making algorithm for third right and algorithm to find rather than number is even or so again you can use decision making algorithm here. And for fourth right and algorithm to print total marks percentage and division again decision making algorithm, but input output will be different. Then last one is to calculate factorial for sure you will be using iterative algorithm. So for first you will be using sequential or second, third and fourth you will be using decision making and for fifth one you will be using iterative algorithm. So try to implement these at your own if you find any problem or difficulty so come in under this video or you can open my channel and find my contact details details also so you can discuss with me email me like what whatever problem you are facing. So try to implement these algorithms with these examples those I explained you and I hope you understood whatever I explained in this video related to how to write an algorithm. So that we can find the solution of a problem. So keep watching these videos related to data structures and algorithm. So I will be soon uploading more videos related to other topics as well like arrays link list that you see in graph. So watch them and learn data structures and algorithm. So thank you for watching this video.