Hi everyone, welcome to this computer class on flowchart and algorithms. This is a very very important topic because in today's class you are going to learn how to think like a programmer, how to think in terms of flowcharts, algorithms, we will also look at pseudocode, how to write the program before you start coding. So very very important class and I am going to make this concept super easy for you.
So let's get started. Before we begin the class, I want to say do check out the other courses on our website. We have Physics, Chemistry, Biology and Maths for CBSE class 8, 9 and 10. So guys, if you haven't taken the other subjects, do take them and do share it with your friends.
For the ICSE students, once again, we have Physics, Chemistry, Biology and Maths for classes 8, 9 and 10. So do share out our courses once again. And as you know, we have these two coding courses, Python and Java. Both are very, very...
popular computer languages so definitely you're learning the top programming languages used by professional programmers today and we also have cambridge igcse which is the international board we have physics and chemistry courses there so please do share the courses on our website make sure you have subscribed to our youtube channel we also have a hindi channel it's called manucha academy hindi and you can follow us on facebook and instagram so do stay connected with manucha academy and keep learning so today we are going to learn about how we think about the recipes right so what is your coding recipe or first let's start with a favorite example i'm sure all of you love eating noodles right so if you need to if you are hungry actually looking at this picture i am feeling hungry but if you are feeling hungry and you quickly want to make some noodles or your mom is making noodles she you or she follows a recipe right so to everything that you do in the kitchen there is a recipe or if you're baking a cake there is a recipe so what is the recipe when you are making the noodles can you guys tell me so what do you need to do here so let's say you're making these instant noodles so what is the first step obviously you will cut the packet and boil the water right so we can say we open the packet we boil the water and then obviously you will add all the noodles in the boiling water and then after it's mixing now you pour in the masala the spices right so it's more like a cooking class today so then you add the masala or your spices and you keep cooking it till it looks nice and yummy and you stop it and enjoy the noodles So this is the recipe right? These are the steps that you follow while making noodles or instant noodles. But suppose can you change these steps? Can you you know add spices first, then open the packet, then boil the water?
You know if you do that you won't get these tasty noodles and especially in cake recipes. If you make one small mistake either the cake turns out to be hard. or you know there's no sugar in the cake it's not sweet so any ingredient or any step you miss the recipe is gone right so that is why just like in the kitchen you follow a step-by-step procedure a recipe similarly when you're writing code when you're writing what is code? Code is instructions to a computer to do something you are giving the computer the recipe or you are writing the recipe for the computer so that it does exactly what you wanted right just like exactly what you expected to the noodles to taste you get it if you followed the recipe or you know when the cooking thing goes wrong mom will say you didn't follow my recipe similarly we are going to learn how to think about not cooking recipes coding recipes today So we are going to talk about code recipes today and code recipes can be written in different ways. What is the meaning of a code recipe?
So the moment you are, you know, let's say you want to design a program, whether it's a game with rolling of a dice or whether it's, you know, doing some income tax calculation or finding out, you know, the average marks of a test, any of these things, always you don't immediately start writing the code especially the program is a little complicated right what you do is you think of what will be the steps you break down the code into steps and those steps need to be in the correct order so that is what we call in you know informal way as code recipe now code recipe can be written in different ways it can be expressed as a flowchart you can write it as an algorithm or as pseudocode and we're We are going to discuss all these three styles of recipes today. So very very important class. So at the end of the class you will learn to think like a coder, like a programmer and these techniques will help you plan out your recipe before you write the code.
So what we are learning today is not coding, we are learning the thinking, the planning step. So in code recipes what we are learning is planning and Thinking of the logic and you think this is important this is super super important because if your logic is wrong just like if the building plan is wrong the architects plan is wrong the building is not right same way if your logic or your plan is wrong obviously the code not do what you want it to do so thinking like a programmer is very important and let's learn these techniques which will help us express the recipe so it's important for us and for anybody else who wants to know what our program will do he may not know the language that you're coding in like for example he may not know Java he may not know Python same way here If they look at your recipe, they will get to know even though they do not fully understand your code. It's just like, you know, when JK Rowling was writing Harry Potter, she was brainstorming and thinking of the ideas. What story to write?
It doesn't matter whether she wrote it in English, whether she wrote it in Hindi or, you know. Spanish, German because now it has been translated in all the languages of the world, right? So the language doesn't matter. It is the thought, the ideas.
So same way here, it's not the coding language. It's the recipe that matters and we are going to discuss about it. So we are going to understand what is a flow chart algorithm and pseudocode with a very simple example. Let's say the question given to us or the problem given to us is find the area of a circle. So we want to write, the goal here is we want to write a computer program to find the area of a circle.
We will start with a very simple example. Now when you want to find the area of a circle, what does the computer need to know? First of all he needs to know how big is the circle, so what is the radius of the circle right?
So for example here The computer needs to know what is the radius of the circle so that must be supplied to it and then you calculate the area so the radius of the circle has to be input to the computer and then it will work out the area using the formula area is pi r square but does the computer know what is the value of pi? Computer doesn't know. We have to supply it. What value you want to use? 22 by 7, 3.14.
What value you want to supply here? Usually in computers we use decimal values rather than fractions. So maybe you'll be substituting 3.14 here. So the goal is clear we need to write a code or a program to find the area of a circle but we are not going to do the coding we are going to do the planning of what will the program do using flow chart algorithm and pseudocode. So let's take a look at because these are all different styles of writing the logic the recipe of the program.
So let's understand what are flow charts. So as the name suggests it's a flow chart which means it is like a chart where you can see see the flow of the program so what is the formal definition of flowchart it is a pictorial it is a pictorial representation of step-by-step solution of the problem so pictorial means graphical so you can see you're sort of drawing a picture of the steps showing it like a graph so it is more of a kind of graphical representation or a pictorial representation of the step-by-step solution of the problem and this is what we call as a flow chart now when you're writing a flow chart there's one interesting thing that you use different shapes or different kind of boxes here you don't just put everything into a rectangular box you can see that different kind of shapes here for example can you see the start and stop is kind of rectangular box but with kind of curves at the end so it looks like an oval almost so start and stop is there and this signifies the start of the program and the end of the program stop then when you want to whenever you want to take input Remember we said for the computer to calculate the area you need to supply the radius and let us say we are taking user input what you have learnt in Java or Python you can take user input. So the input box we represent with this parallelogram kind of symbol.
We do not use a simple rectangle here we use this parallelogram to input the radius. So you can see start the first step is to input radius because you not do first step area if you don't know the radius how to calculate the area then so the next step we represent we draw this arrow so the program is flowing like this you can clearly see the direction in which the program is going area so you put down the area formula 3.14 x r x r see now you're getting a little more computer specific because computer may not know what is r square right so you're writing the clear formula for the computer So in these kind of things sometimes you keep the computer in mind. Sometimes in the books you will say they are simply writing pi r square in mathematical terms.
So whatever convention is being followed in your school or your book. But usually we try to write it close to the computer. Here sometimes you might want to use multiplication because you know in computer the multiplication is represented with the star symbol.
But for here, I have ignored that detail. We have just written it as a normal multiplication. So that is your area calculation. And then finally, you go to printing out, printing out the area.
So can you see again, you have drawn a kind of parallelogram box. Why? Because this input and output are drawn, represented with this parallelogram.
And any processing or any computation that happens, represented with a rectangle. So clearly you can see this is the input and output steps. So this is an input step, this is an output step and this is the processing step and then finally you end up with the stop where you write stop or sometimes you might see the word end here to show the end of the flow chart. so these are the standard flowchart shapes and symbols and when you're writing comments you write this annotation with this kind of you know line and then this kind of bracket so these are your annotation or comments in the flowchart we look at some more symbols these are usually standardized symbols in fact I pulled these symbols using the there is a flowchart tab in PowerPoint also so these have been drawn using PowerPoint's flowchart shapes so they're pretty much the industry standard today And as you can see, it is a pictorial representation of the solution to your problem. So this is how you write down the flowchart.
Flowcharts are very popular because it's very easy to read, very easy to understand the flow of logic. And it is one of the most popular ways of representing a recipe or your logic. So we saw an introduction to flowchart next let's look at what is an algorithm. Again it's another way of representing your recipe your logic.
So what is an algorithm? It is a set of steps to solve a problem. algorithm is nothing but a set of steps to solve your problem just like we saw the algorithm for cooking the noodles right step-by-step procedure and here you listen down very clearly like one two three four five just like recipe steps you can just write one two three four five or you can write the word step one step two step three whatever convention you're following these are not written in stone you know you might change yours you might see changes in styles in different books so what is the first step again like a flowchart so this one is not pictorial it's more like a step-by-step statements so you can see start and the ending one is a stop then what do you have you say input radius and you give your variable name here comma R So this is more written like you know in English rather than writing the actual input statement.
So like in Python the input could be different. In Java you use scanner. In C++ you use cin. So we are not bothered about that. We are writing it in simple English input radius r.
Next we will say. How does the computer calculate the area? We give the formula and typically in algorithm you will see rather than using equal to they use this arrow symbol so they're saying basically 3.14 multiplied by R multiplied by R is assigned to this area variable. So this is very popular if if your school or your book is using equal to that's also fine. but usually the arrow symbol is popular and the arrow is from right to left okay and then you go ahead and print out the area otherwise the user won't be able to see the output so you can say print area and your variable name a so you can see all the variable names are clearly mentioned and then you stop okay sometimes this arrow symbol is also used in flowcharts so you can see it's very similar to a flowchart very very similar the main differences are we don't draw a diagram we just Write it in terms of clear steps.
Steps have to be numbered, very important. Because you might say, now go to step 3. Just like in a recipe, you know, sometimes they say go back to step 2. So step numbering is very important. and then you write it in very simple English. You are not writing the actual code like Python or Java because maybe your friend doesn't, let's say you know Python but your friend knows Java.
So we want to write it in a simple, friendly, neutral way. So that's why you write it in these kind of sentences and this kind of arrow and some conventions are used. And you clearly mention your variable names.
So is this simple algorithm clear to you? So we have looked at flowcharts and algorithms. Next let's look at pseudo code.
What does the word pseudo mean to you? Do you guys know the meaning of the word pseudo? The word pseudo means fake or false, right?
So this is like fake code, almost like, you know, not the real code. But the advantages it looks very similar to code. So we can easily translate it to another language.
So what exactly pseudo code? Pseudo code is an informal way, see the keyword here. It is an informal way of describing the steps of a program solution without using a strict programming language syntax.
So it says without using a strict programming language syntax. What does this statement mean? so you're not writing real code because real code you know is very very strict it has a very strict syntax if you're writing python or java code you have to write it perfectly correctly otherwise the compiler will give an error but this is not like that this is just writing this is like pseudo code is more like a rough code ok so you're just writing it roughly so we can say pseudo code is like a rough code you're just writing it informally the advantage is that you can quickly write it down like you know rough code and then translate it once you're satisfied with your logic now you write the actual code so it will save you time because you are not wasting time thinking about the syntax and details of the language your focus is on the logic so that is the whole importance of this class that when you are learning flowchart algorithm pseudocode your focus is on the logic and not the details and nitty gritties of the language.
Okay, that comes next. You are in the planning stage, right? So same way here you are saying input radius in variable R.
Okay, this is one way of writing. You may see different ways. Sometimes they'll just say input radius comma R or in variable R, whatever style you're seeing.
There are different styles of writing pseudocode because it's all informal. There's no right or wrong way. Similarly, you can say area equals 3.1. 3.14 times R times R.
So you have clarified your calculating area or you can maybe just write capital A, display area. So can you see the logic is very clear that we are doing input, calculation of area and display. This is called pseudocode. So are all the three styles clear to you? Flowchart.
algorithm and pseudocode. We will of course look at some more examples to really understand these concepts but the whole goal is to plan out your logic to brainstorm and decide your logic and especially if the program is complicated so you do this and this is what professional programmers do they plan it out using either flowchart algorithm or pseudocode then they go to the actual coding because actual coding takes time it needs to be perfectly right but before that your logic needs to be right. So in summary this is what we have learned that using the example of area of a circle you could use flowchart algorithm or pseudocode. So I put them all in one slide for you to see that the whole goal here is to represent the logic.
So what we are trying to show here is the logical steps. So flowchart as you can see is a pictorial kind of representation. You could write an algorithm which again has start-stop but you're writing it in a stepwise fashion and pseudo code is like your rough informal code.
Sometimes your school will teach you all these different styles or sometimes you might be following one style. Many times, you know, sometimes people find it easier to first draw the flow chart, then write the code. Sometimes even some people find it easier to first start off with the code and then start thinking of the flow chart, then again come back to the code.
So there's no perfect right or wrong way. It is what style you like. But always they recommend that do not jump to coding first.
Do the planning. running on paper first or at least in your mind before you go to code. Another very important aspect is verifying your algorithm or your flow chart or your pseudocode.
What does this mean? So let's say you have written an algorithm. Now you may think that it's right and you'll say, OK, I've written my algorithm. Now let's go to the computer and start coding because we all love to code, right?
We want to get our hands on the keyboard and write the code. But wait, there is a smart way to first verify your algorithm on pen and paper using pen and paper. and then code so this is what we call as doing a dry run or using trace tables so what is the meaning of dry run that the code that you have written or not exactly the code the algorithm that you have written you are doing a check of it on pen and paper before you write the actual code so how do we do that very simple you become the computer now and go step-by-step in the algorithm and check if you're getting the desired output so for example let's start with step 1 so step 1 is simple start then we say let's input the radius so you keep your finger here you're in step 2 and in the dry run we'll say We are going to input the radius.
So let's take some example. Let's say the radius is 10. So we have come to step 2 here. Then step 3, area is 3.14 times 10 times 10. So what is this going to be equal to? 314. then the next step step number four so please go step wise we have finished step three we are going to step four here now step four is to print the area so the computer is going to print 314 here and then we stop so can you see the program is working exactly as we expected let's say we input 10 it calculated the correct area and printed it out so this is known as dry run doing it on pen and paper without the computer here your brain is the computer so this kind of procedure is called dry run and the important thing in dry run is make sure you keep your finger or you keep ticking as you are doing these steps so you keep going step by step so that you do not miss out any single step another way is using trace tables What are trace tables?
Trace tables are basically you know while you're running the program or while you're doing a run on pen and paper seeing how the values of the variables are changing because you can see the important thing are the variables. Radius then area. These are the important things right.
So what will you do here? So again you do start. You come to your first second step which is input radius. So you can see that. variable and you can say value so variable is r you've created a variable like this and let's say the input value is 10 so we'll put it in our table here after that we move on to step number three what is the next variable here area so we put the variable a and what value will it take 3.14 times 10 times 10 so it takes the value of 314 and then we are printing area so you can keep an output column here also so after this stage the output will be 314 So this is known as trace table where basically you are seeing that all the variables that are there in your memory or in the computer's memory, what are the values, how are the values changing and what is the output.
So this is how you verify an algorithm very easily using pen and paper without running it on a computer. And it's very important especially for the complicated ones to verify it using some simple examples. sometimes if the they have different edge cases or difficult cases like if let's say some program can take a negative number then you should try a positive number and a negative number and make sure it works for both cases it's not good if the program is only working for one case right so this is what we call as verifying checking if your algorithm or your flowchart or your pseudocode so you can do this for anyone checking if your algorithm the step-by-step procedure is right So now we have learnt all about flowchart algorithm and pseudocode. Are you guys ready to pick up your pen and paper and try these out practically. So let's go ahead and do it and we'll start off first with the problem.
Find the smaller of two numbers. So simple question let's say you want to input two numbers from a user. You want to find the smaller one which basically means. the minimum and now what we are going to do is we are not going to write Java or Python code we are going to write the recipe using flowchart style algorithm style and pseudocode let's start with flowchart first so before we look at the flowchart let's quickly discuss the types of flowchart symbols I showed you a simple example now let me formally write down the names of these symbols so this first one which looks like a rectangle but it has this kind of curve so almost looks like an oval but not really an oval here this is known as start it's used for start stop so it is often called terminator Have you guys seen the movie Terminator? I think it's one of my favorite movies, right?
It was released I think when I was your age or something. So Terminator, these are called Terminators because they start and stop the program. Next one what do we have here? This parallelogram kind of looking thing. What is this known as?
So when you have this parallelogram shape, this is for input output, right? This is to do input or output. So this guy is used for input.
So it's our input or output box. and the next one here the rectangle one remember which contain the formula what is this this is the processing box or computation normally we call this the processing box used for processing so you must follow these conventions these symbols the next one if you have this diamond shape kind of thing do you know what is this used for and we will be using it in our examples this is used for condition or decision a yes no so you put your condition inside this and then then you can create branches so this is used for condition or what we call as decision box Then you have this symbol where you just have a simple circle, this small circle. So these are used as connectors. What is a connector used for? For example, you are writing a flow chart and then you run out of space, right?
So you have reached the end of the page. Now you want to continue to the next page. So how do you do it?
You use a connector. So let's say you are writing a flow chart and then you end with this connector and you can label it something like 1 or 2. So you put the number 1. here and in the next page you continue you put the connector again right one and then you continue your flow chart so connector is used to connect flow charts on different pages so it's more like you know continuing the flow chart Then you have these arrows. Remember flowchart the boxes need to be connected with arrows not with simple straight line with arrows. Because you are showing the direction of flow of the program. So these are known as arrows which represent flow of control.
So these represent for us. Flow of control is our program moving downwards to the left to the right that's how you represent with the arrows and this last one remember I use this this is to write comments or your annotations so this is used for annotations comments to your flowchart so these are the most important ones there are of course more flowchart boxes and symbols but these are the most important ones that are done and will be using them today so are you guys ready to draw the flowchart for find the smaller of the two numbers so that's our question So how do we start? Remember flowchart is simple, it starts with the terminator start.
So let's do that first. So we are going to write start and please put it in this sort of box. now when you want to find the smaller of two numbers clearly you need to input two numbers from the user and then find the smaller so we need to input two things here so let's make the input box first please draw the arrow and input is shown using this parallelogram kind of symbol now you can draw two boxes and write input A and then input B or you can do a shortcut and simply write input A comma B See we are trying to do a flowchart like this. Now sometimes you will see in the books they use all uppercase letters.
Sometimes lowercase in flowchart. Doesn't matter whatever style you like. So since I have used some lowercase here.
I can you know write start in lowercase as well. Just to keep it consistent. And please connect these boxes with the arrows. So our input is done.
We have input two numbers a comma b. Now what is the next thing we need to do? We need to find the smaller of two numbers and obviously print it out.
So how do we do that? Which symbol should we use here? So now we have to ask a question.
Who is smaller or who is bigger, right? So we will use this, you know, decision or condition box. So we are going to use that. So the next thing will be, it will flow into the decision box. where we are going to ask the question is a less than b and you put a question mark and then you can draw this decision box around it is a less than b so again see written in simple english right not any fancy code So now if a is less than b there can only be two answers either a yes or a no.
So from a decision box you can have a yes or a no. Computer doesn't like maybe or not right. It's binary yes or no.
So what will you do here? So you can have two things coming out either a yes or a no. So two things flowing out. Now you could flow the two things out of here as well. So you could have your yes and no here, whatever style you like.
So if A is less than B, what value will you print? We want to find the smaller the minimum number. So obviously we are going to print A. So let's write that down. So once we have this, you're obviously going to.
So you'll be printing A. And if the question is a is not less than b, who will you print? Obviously the value of b.
Clear? And we put this in parallelograms because we are doing an output now. Now some of you might be wondering what about the case if a is equal to b? So let's say both of them have the value 10. So let's say a is 10 and b is 10. where will this flow chart take us so when you reach this question is a less than b is it going to be a yes or a no if both are 10 because 10 is not less than 10 so obviously the answer is a no it will go here and it will print b which is 10 but it doesn't matter whether you print a or b because both are equal so both are correct we don't need a third case here because if both the numbers are equal any one of them is the minimum see this is where flow charts help you this is with thinking of the logic helps you to make sure your logic is right before you code and it is clear from here that you are still going to get the correct answer now how do you finish it off so one you know one way is to write this so you do a stop here and similarly you will create a stop here but is this a good flowchart no because there should only be one start and one stop this is not a good style please do not do it you cannot have two starts two stops looks very weird so what do you do here you just connect these two so please do not create two stops so what you should be doing is just join these two so the flow of control will go this way and then you create your stop here there you go so this is how we write our flow chart is this flow chart absolutely clear to you Next we are going to write the algorithm for the same question because we are going to practice all the things flowchart, algorithm, pseudocode. So what is the algorithm going to be?
So remember algorithm means step by step right. Step 1, step 2, step 3. So you need to write it in this style. So please think how will you write the algorithm and again you are sort of writing it in English and expressing the formulas if needed.
So in the algorithm again we're going to write the step-by-step because it's a step-by-step procedure so step 1 we're going to say start sometimes you know this start stop is ignored in the algorithm but better to write it down so that shows the start of the program what will be the next step input the two numbers a comma B now you can write input a next input B or you can put it in one line so you can say input a comma B or you can even say input two numbers a comma b right and then what will be your next step step 3 you want to find the smaller of the two numbers so again here you're going to give the decision so what are you going to say if a is less than B You are going to ask this question right is a less than b then print a else or otherwise right whatever style you are following else print b. So this is our decision step you can see clearly we have written if a is less than b. then print a otherwise print b there you go that's our algorithm and then we can end it with step 4 stop so this is how you write your algorithm of course it could be written in slightly different styles you may not want to write two numbers you can just say input a comma B or X comma by whatever and this is how you express the if condition you can put it like this in one line if this then this otherwise or else do this so do you guys agree this is the correct algorithm for our required program to find minimum of two numbers if it is maximum you just simply change the sign to greater than that's it now let's try to write the pseudo code so what is the difference here So remember pseudo code is informal way of writing code, right? Where you say things like input radius in variable r or you can say input two numbers in variables a, b. then again you do your you know whatever computation or decision and do your printouts so it's an informal way where you're not following any particular language you're trying to write it in sort of simple english and express some formulas if needed and express your logic so let's go ahead and write down the pseudocode to find the smaller of two numbers so here usually we don't write one two three we just simply write the code here so what will it be input two numbers in variables a comma b or you can use capital AB notation whatever you like so that is your first thing you have taken care of the input now you're going to write that decision so we write it more like an informal coding style here so we can say something like if a less than b If you want you can put a bracket here as well.
So if a is less than b then what do you do? Then you print a or you can write display a else print b. So this is actually very similar to Python code except in Python code you have certain colons and stuff but you can see we are writing our logic in a simple way not caring are we coding it in Python, C++, Java or whatever language right.
So we don't care about you know semicolons and putting all those curly braces for ifs and else we just write it in a simple logical style. Again, there is no right or wrong way of pseudocode because remember it is informal way. So your way might be different from mine or what is being taught in your school.
But use whatever convention and try to keep it simple and easy to read. Okay, so there we print it out and there you are done. The code is ended here.
So that is our logic. You can see we are making the output depending on the condition here. So is this pseudocode simple and easy for you now? No.
So once this code is pseudocode is written it's very easy to translate it into real code in a particular language. So those were simple example that is a simple example and now let's try a more interesting example which says find the factorial of a number and again we are not going to write code we are going to use flowchart algorithm or pseudo and pseudocode all the three styles to express the logic here. But first of all what is factorial of a number do you guys know this is a mathematical term.
So what does factorial mean? So factorial is a mathematical term which means that let's say you want to find factorial of a number 4. So how do we represent it? Factorial of a number.
Let's say 4 factorial is represented as 4 exclamation and it basically means 1 multiplied by 2 multiplied by 3 multiplied by 4. So you multiply all the numbers till that number. So 4 factorial is basically 1 times 2 times 3 times 4. So what is the answer going to be? 1 times 2 is 2, 2 times 3 is 6, 6 times 4 is 24. So 4 factorial is 24. Similarly, what is 5 factorial? 5 factorial is going to be 1 times 2 times 3 times 4 times 5. So this is going to be 120. Please multiply and check.
this is the meaning of factorials if anybody ask you know what is factorial of a number you know you keep multiplying the number till you reach it and obviously you can see the numbers become big because you're multiplying so 5 factorial is pretty large 120 so let's see how will you write a program to find factorial of a number obviously you input the number from the user so let's start off using flowcharts to think about the logic. So what is the first thing in flowchart? Obviously we will start with start.
So we simply write start here and let's keep this example on the side. So 5 factorial is 1 times 2 times 3 times 4 times 5 which is basically 120. so that you guys remember this example so first we do start then the next thing will be to input the number whose factorial we want to find let's call that number n so the next step in the flow chart will be to input n so here for example I'm using capital letters for variable doesn't matter whatever you want right so we are going to input the number n Now, once we have input the number n, what do we need to do? We need to multiply all the numbers from 1 to n. Yes, 1 times 2 times 3 times 4 and keep on going till n.
So, how do we keep on going? Because we need to start from 1, that means we need another variable to keep as a counter. Do you guys agree? So, basically, we can think of we need a variable, let us say i, which goes from 1, 2, 3 all the way till n.
So this i is basically changing. Yes. And then we are storing that multiplication, let us say in the factorial of the number, which we can call as f. This stores the factorial. So when you are multiplying the number, it keeps storing that.
For example, if you have multiplied till 3, it stores it till here. Yes. So, after we do the input, what is the next step? We are going to create these variables. i is 1 because we start from 1. and this factorial which is going to save the factorial since we're doing a product just like if you want to find sum of numbers what does the sum start off with 0 but now we're doing a product if you start off with the initial value 0 whatever you multiply it's going to be 0 so be careful here so our other variable f will also be 1 so this is our processing box because we have not taken these as input these are the computer variables.
Now what is the next thing you will do here? So to calculate the factorial, what will be the formula? The factorial is basically the current factorial times the next number i.
If you look at the logic here, so suppose you are doing 1 times 2 times 3, so you keep stored that in f, then you multiply it by 4, then again you store that in f, multiply it by 5. So can you see the formula just like sum equals sum plus i? Let's say you want to find the sum of numbers, just like sum equals sum plus i, this is to find the sum of different numbers. To find the product, what will you do? f equals f times i.
Okay, same logic here. So what we're going to do over here, f equals f multiplied by i. But now what is the next step? You need to keep on increasing the value of i.
1, 2, 3, 4, 5. So i just started off with 1. So what is the next thing we are going to do? We are going to say i equals i plus 1. That means increase the value of i. Now what do you do?
So once you have increased the value of i, you want to go back to the formula. You want to loop back and go here and keep doing this. but if this is your logic the problem is you're never stopping can you see your you are in an infinite loop here you are not stopping based on the value of i eventually stop when you have reached n because you need to do the multiplication 1 2 3 4 5 till n so you need to ask yourself the question you cannot simply loop back here We need to ask ourselves the question after increasing i, is i greater than n?
So ask yourself this question, is it greater than n? If the answer is yes or if the answer is no, then you simply loop back. So you're allowed to go back and continue this multiplication. So if your answer is no, you're going to do this. But if your answer is yes, I am greater than n, then what happens?
You will simply come out, right? You will come out of this loop and See since I have run out of space maybe I can use a connector here. So I can call this connector 1. And finally we will print the value of the factorial f which is stored in the variable f.
And then we will end the program. See I am checking i should continue to be n. So let's check if our flowchart is correct. So for example let's do a dry run here and see this. So let's say we have, we start off with i equal to 1 and factorial f is also 1. So then what happens?
And let's say the input number is 5. So first time what is going to happen? f equals f times i. So 1 times 1, it remains 1. Next time i equals i plus 1. i becomes 2. So it asks the question, is 2 greater than n which is 5?
The answer is no. It goes back. So now the factorial becomes factorial times i. So 1 times 2, factorial becomes 2. Then again we do i equals i plus 1. i becomes 3. Again we ask the question, is 3 greater than n?
No. We loop back. So factorial becomes 2 times i, 2 times 3, 6. Again we do i equals i plus 1, i becomes 4. Again 4 is not greater than 5, so we loop back. Factorial is factorial. times i.
So what do we do? 6 times 4, 24. i equals i plus 1. i becomes 5. Again we ask the question, is 5 greater than 5? No.
So we go back. Factorial is factorial times i. 24 times 5, 120. Again it says i equals i plus 1. i becomes 6. This time 6 is greater than 5 so it will come out.
It prints the last value of the factorial which is 120. As you can see 120 is the value here. That will get printed out and then we stop. So can you see we did our test run and it worked out perfectly. So your condition is very important.
You need to check is it greater than n. Then only we stop. If it's equal to n we let it go because we want that multiplied here. So is this flow chart clear to you guys?
now similarly if you have to write the algorithm for finding the factorial so how do you guys do it again this is step by step so we will write down the steps here for example step 1 start step 2 is input your number whose factorial you want to find step 3 you assign these values i equal to 1, f equal to 1. If you want you can break this down into two steps where you are doing this initial assignments. Sometimes we said when we are showing assignments in this algorithm we tend to show it with the arrow. So whichever style you want to follow.
Then what is the next thing we need to do? Now we need to write the formula to find the factorial. Factorial is you multiply the current value with the next i. So we are going to write the formula. Step 4. Factorial f is f the current factorial multiplied by the next number i.
So we did that. Next step will be. to increase the value of i. Yes or another thing we could have done is we could have checked the value before increasing it. So depends on how you are writing the loop.
So here we can do i equals i plus 1 which means increase the value of i and then put it back in i. Now what will step 6 be? as long as your value of i is less than or equal to n.
So what can we say here? While i is less than equal to n, you need to keep repeating these steps, right? So as long as i is less than equal to n, you repeat steps 4 to 5 or 4, 5, 6, right?
So while i is less than equal to n, repeat your steps 4, 5 and 6. So see algorithm helps us to tell which steps to repeat. This multiplication step, increment step and again the checking step. So keep doing this and then once it is more than n obviously we will come out and please don't forget to print the value of the factorial.
So we will print out f and step 8. It is stopped. So there you can see we have written the algorithm of the same program. The whole logic is, so the whole trick in factorial is you keep multiplying the number with the next i.
So just like you know when you are finding 4 factorial, what do you do? 1 times 2, see, times the next number. 3 times the next number, 4. So same logic here. You keep multiplying it with the next number. Keep incrementing your i and do this.
Repeat these steps 4, 5 and 6 as long as your i is less than equal to your number n. The moment i becomes bigger, you print out the thing and you end the program. This is how you write the algorithm. So yes, I think many of you might be thinking flow chart is easier.
Definitely, because it's a graphical pictorial representation. But the problem is, you know, you have to draw these boxes, all these arrows. So it's a little more time consuming, but definitely more visual than writing these steps. And last thing we're going to try is writing the pseudocode of the same question so that we are clear about all the styles and you can decide which style you like. So in pseudocode, remember, what was it like?
Pseudocode looks like a... program right informal program so let's go ahead and write that so pseudocode of our factorial is going to look like so what is the first thing you're going to do input number in variable n again you can use small or capital depending on the convention so here I'm using capital just to keep it consistent so capital N is our number whose factorial we want to find so what will be the next step so we will of course assign the values here we will say I equals 1 and our factorial is 1 you can break this down into separate lines also if you want So what is the next thing you are going to do? So basically you need to do the check that as long as i is less than n.
So you can say while i is less than equal to n. So in this kind of style you might be thinking of writing the check first. In flowchart we wrote the check later. So it depends on your logic, right? How you are writing it.
So here you are saying that your first checking is your number less than equal to n. as long as i is less than equal to n you will continue doing this so in the factorial you will multiply f equals f times i and i equals i plus 1 okay so these you're doing inside this while condition and sort of your while loop ends here so you can write end of while again this is informal style to show that this is where my condition or looping started so you can see clearly there's a loop thing here because you're repeatedly doing the steps so can you guys see that we are in a loop just like we say playing a song in a loop repeatedly playing it so repeatedly you're doing the steps but but it's not an infinite loop you're not going on and on there is some condition here similarly in the algorithm you can see there is a loop here because we are saying repeat the steps four five six as long as this condition is true same thing we are saying here can you see there is a while there's a kind of a looping thing here going on keep doing this as long as i is less than equal to n. And then you come out of that.
Once you come out of your condition, what will you do? You are going to display or print the value of not i, the factorial f, and that's when you stop your. program ok so again you can see the nice part about this is this is quick code rough code but it is very close to almost writing the code so pseudo code the advantage is that you can quickly translate it into Java or Python code because you have almost written code ok algorithm the advantages it's very clear step-by-step and you have it outlined all the steps in a systematic fashion and you can easily address the step using their step numbers and flow chart of course lot of hard work drawing all these boxes but very visual and pictorial and you can clearly see the flow of control so that's why always remember to put the arrows so which one did you like flow chart algorithm or pseudo code what is your favorite here so here you can see this is the summary slide like we discussed we have discussed all the different ways of putting down or writing our logic we can use flow charts like this we can write it in a algorithm step by step style or we can write this informal pseudocode but good idea to practice all of this and of course when you're doing you know you're writing a real program you can follow the one which which is your favorite which one you like whether you like drawing a flowchart like this or writing and down an algorithm or writing it terms of pseudocode but the goal here is to brainstorm and get your logic right and as I showed you you can do a dry run or use truth tables and verify on pen and paper that yes my logic is right so you're confident before you sit down for your actual coding because you know coding is time consuming you have to write it perfectly and when you run it and then you see it's not working you know it feels disheartening so that's why plan and get your logic right first and then go to the coding step so hope you guys enjoyed this class where we learned all about flow charts algorithms and Very, very important class in computers.
So now you guys can think like a real programmer, like a real coder and become more expert at programming. And do check out the other courses on our website. You know, we have physics, chemistry, biology and maths for CBSE class 8, 9 and 10. So guys, if you haven't taken the other courses, do take them and do share it out with your friends. For the ICC students, again, we have physics, chemistry, biology and maths for classes 8, 9 and 10. So once again, do share out.
these courses and as you know in computers we have python coding we have java coding both are very popular computer languages used today and for cambridge board the international board igcse board we have physics and chemistry courses so do share out our courses with your friends make sure you have subscribed to our youtube channel you can also subscribe to our hindi channel it's called manucha academy hindi and do follow us on facebook and instagram so stay connected with manucha academy and keep learning