Fundamentals of Programming – Mastering Functions with Examples | Programming Basics for Beginners

GeeksforGeeks · Beginner ·🛠️ AI Tools & Apps ·1y ago

Key Takeaways

Explains the fundamentals of programming using functions with examples for beginners

Full Transcript

Hello, hello, hello, guys. Welcome to the stream. What is up? But Never mind. Hi, Krishna. The one hello. This is what we are planning to do. Hello Sandeep. I'm good. How are you? Mark, no, you're not late. I just started. All good. You were not there in yesterday's stream, Mark. Uh you were not there in yesterday's stream. Yep. Uh Krishna, we'll dive into it in exactly 2 minutes. We'll wait for everybody to join in and that's when we'll dive into it. Let everybody join. Write hi in the chat. It is going to be very, very fun today. The agenda for today would be Where's my pen? Uh the agenda for today would be um pattern questions, a few patterns that we'll code. And then after that, we'll jump on to functions. These are the two two things we'll be covering today. I have a very few streams left um on this channel. After that, I will not be streaming. So, it's only till next Wednesday that I'll be streaming. Next Wednesday I won't be streaming on the channel. So, yeah, every day 10:00 I will be seeing you guys. And okay, what what country are you from? Mark. What country are you from? Like it though. Like the stream. Come on. Hit a like. Killer, best way to practice coding is doing a lot of questions and uh doing more easy questions. And then a little lesser amount of medium and a little lesser amount of hard questions. Always code in a pyramid. Keep this as a tip. Always whenever you're programming code in a pyramid. This is the way you should do. Easy, medium, hard. Taking an example, if you're doing 20 easy questions of a topic, do around 15 medium questions and 10 hard questions. This would get you to a very good place, for sure. Oh, nice. Good. My current manager is a Filipino guy. Mark. Chalo. Let's uh dive into the stream. Okay? So, the first question that we'll be doing is a diamond pattern question. Okay? Let's Let's see what we're going to be doing. It is uh something like this. If we have to draw something like this, what kind of code would we do? Anyone who can guess? What should be the code if you're coding this um pattern? What kind of logic are we going to use? For those who didn't attend my uh last two streams, last two Yeah, last two streams on patterns, I've told you one very simple thing that we have to remember max to max two kind of patterns for such triangular pyramid kind of patterns, which is an increasing triangle and a decreasing triangle. What do you mean by an increasing triangle? That the number of elements in the triangle, they increase. In the decreasing one, the number of elements in the triangle, they decrease. Understood? Now, the point is when you try and draw something like this, something like this diamond, imagine it to be a combination of four different patterns. Python and C, I would have, but uh Upadhyay, sadly, my streams end next Wednesday. I won't be streaming after that, so sorry. I'll be I'm teaching C++ you can you can learn C++ from me, but that's the max I'm doing uh on the channel. Cool. So, when we when we talk about this pattern, this is a combination of four different patterns. I'll tell you how. Okay? One. Two. Three. And four. And I'll explain how this works. See, if I print a decreasing star or a decreasing triangle pattern, if I code a decreasing triangle pattern which only prints spaces, okay? And then I print an increasing star pattern that prints stars, but it also prints a space with a star. How? Star, space. Star, space, star, space. Do you see? So, it's printing that pattern that we need. Right? And then to carry on the diamond, we again print a increasing star pattern with spaces. And we print a decreasing star pattern with space. Might be very confusing for now, but you'll understand this when I code it. See. Did you guys understand the the agenda behind this? Did you guys understand what I'm what what I'm trying to do here? Yes or no? Is this understandable kind of a logic? All you need to know is increasing star and decreasing star. That's it. And then you combine it in ways that you get your patterns. Yes or no? Did you understand till here? Tell me. Okay. So, see, if I'm coming here and I'm writing something like this. Yeah, it's a combination. For So, I'm going to say int n is equal to five and for int i is equal to one i is less than equal to n i plus plus. Okay? Inside this for loop, I'm going to say for int j is equal to one j is less than equal to n minus i j plus plus and I'm going to see out space inside this loop. Cool? This is going to give me a decreasing space pattern. Okay? No problems? Again, I'm going to say for Okay, let's not close it. Let's Let's just write for int k is equal to one k is less than equal to i k plus plus and this one is going to be used to print stars. Cool? Now, this one star space I'm I'm putting this line here. I won't put it in the code. Okay? And I'll close this for loop. Then I'm going to write another for loop for the lower part of the pattern. And I'm going to say for int I is equal to n minus one. I is greater than equal to one. I minus minus. Why? Because this time I need a decreasing pattern. A decreasing star pattern. I need an increasing space pattern for sure. I need an increasing space pattern. But I also need a decreasing star pattern. So, I'm going to say inside this for loop for int j is equal to one. j is less than equal to n minus i. j plus plus c out space. Cool? And then I'm also going to write for k is equal to one. k is less than equal to i. k plus plus c out star space. And then just outside the loops, c out end l and we'll close the loop. Yeah, I did the big triangle in the last stream. Mark, yeah. Okay? Understood? Yes or no? So, what happens here is basically these three loops give you the first triangle for the diamond. And these three loops give you the bottom triangle and it makes a diamond out here. Let's code this up and see how it looks. And then we'll know if we are right or wrong. Right? So, I'm just going to make a new pattern, call it pattern 18. Okay? Let's say int n is equal to five four int i is equal to one, i is less than equal to What do we say here? Tell me. Anyone? If you guys are watching, tell me. i is less than equal to n, i plus plus. And then four int j is equal to one j is less than equal to n minus i j plus plus. Right? If this loop takes place, we print spaces. We also use a for loop for int k is equal to one k is less than equal to i k plus plus. And we're going to say c out star and a space. Correct? This is what we did. Now, this one is for our upper diamond. Yes or no? Correct? Uh upper triangle, sorry. And the same thing we'll do again and say four in I is equal to one. Uh sorry, n minus one. I is less than decreasing, right? I is greater than equal to one. I minus minus. This time we're decreasing it. And then we'll have four in J is equal to one, J is less than equal to n minus I. J plus plus. C out space. And then four in K is equal to one. K is less than equal to I. K plus plus. Print the stars here. Star and a space. Cool? And then C out enter. That's it. Correct? Understood? Separate task for each triangle to create diamond. Now we talking, we got six loops in all. Yeah, correct. Why did we do I minus minus? Because we're decreasing. The first loop, see, I'll tell you. Check it out. In the first triangle, you're printing increasing stars. That's why you go I plus plus. In the second triangle, you're printing decreasing stars. That's why you go I minus minus. Understood? I've just coded it blindly, so let's hope it works. Cuz if it doesn't, then we'll have to troubleshoot what's going wrong. Uh g++ pattern18 .cpp -o pattern18 .exe Mhm. Perfect. ./pattern 18.exe There you go. That's what you wanted. Right? This is the pattern that we wanted. Correct? That's it. Two different triangles. Two different program uh two different set of loops. Six loops in total. And you get what you need. Understood? Yes or no? Perfect. Now the next question that we're about to do is going to be a hollow pyramid. What's a hollow pyramid? It's going to be something like this. Something like that. Correct? How we going to do this? We are going to just print a pyramid and we'll use an if condition. That's it. So, it's going to be something like that. int Let me open the chat on my phone. Sorry. n is equal to five for int i is equal to one i is less than equal to n i plus plus Inside the loop, we have for int j is equal to one j is less than equal to n minus i, sorry. j plus plus This one is used to print spaces. So, see out space and then for int k is equal to one k is k is equal to one. Okay, it's less than two into Yeah, k is less than two less than equal to, my bad. Less than equal to two into i minus one k plus plus That's it. And then you'll have an if condition. That's going to say if k is equal to one or k is equal equal to into i minus one or i is equal equal n then I'm going to just print a star. Else it's going to be a space. And then finally end it. Understood? Yes or no? So, it is just how you use the loops. Just the way you use the loops to get your answer right. Understood? Let's code it up and see. Let me just save some time. Okay. Let me remove all of this. I'm going to say for int I is equal to 1 I is less than equal to n I plus plus for int J is equal to 1 J is less than equal to n minus I J plus plus C out space for int K is equal to 1 K is less than equal to 2 into I minus 1 K plus plus and say if K equal equal 1 or K equal equal 2 into i minus 1 or i equal equal n c out a star else c out a space That's it. Understood? So that's what is happening. This part prints your spaces. This part prints your stars where you need them. Let's try it out. You got a hollow pyramid. Understood? Oh my bad. I didn't I didn't remove the notes. That's how the code is. I wrote the same code which was there on the notes. And this is the output. a hollow pyramid Understood? My bad. My bad, Mark. I didn't see the chat. I'm sorry. I was coding. No, no, no. all good, all good. Perfect. Let's go. Let's go to the next one. Are we ready for the next question? So, now this one is a little more interesting. And I want you guys to use your brains. Cuz it is a combination of two different questions that we've just done. So, it's something like this. It's a diamond, but it's a hollow diamond. That's it. So, it is a hollow diamond. Means how many loops are we going to have? If it's a hollow diamond, how many loops are we going to have? Tell me. What do you think? Guesses? Any guesses? Six, perfect. How many if statements are we going to have? Because it's a hollow diamond. How many if statements are we going to have? Yeah, with if else, but how many? Any guesses, Mark? Okay. Two, very good. Cuz again, the the skill that I taught you, this time, it's a hollow diamond, which means we have a hollow set of two hollow pyramids, right? One's increasing, one's decreasing. If the increasing hollow pyramid over here in the top took us three for loops and one if else, this one is going to take us six for loops and two if else. Correct? Let's directly code this up. Okay? Or you want me to do a pen paper thing first? Do you want me to write the code on the whiteboard first? Or do you want me to directly code it? Tell me. What should we do? Are we doing it on the whiteboard first or are we doing it Okay. So, let's let's let's do it on the code. How can I make my GitHub repo popular? I think if you post it on LinkedIn, put it on Twitter, market it a bit, that's something you could do. That is um an easy way of getting your GitHub repo famous. Okay. We ready? Pattern 20. Is the text clear on the on the on the VS code? Cool. So, this is what we're doing. #include iostream d main int n is equal to five again. Okay, and let's say four int i is equal to one i is less than equal to n i plus plus Correct? Why am I doing this? Tell me. By now, you should have this by heart that every time you have an increasing triangle, you have i is equal to one, i is less than equal to n, i plus plus. That's it. That is your starting point. If you have a decreasing star pattern, what do you always have? i is equal to n minus one i is greater than equal to one, i minus minus end of story. Let's do it. Four int j is equal to one j is less than equal to n j plus plus and I'm going to use this to print my spaces. Then I'm going to have a k for int k is equal to 1 k is less than equal to 2 into i minus 1 k plus plus Inside this, obviously, we'll have our if statement. If k equal equal 1 or k equal equal 2 into i minus 1 or i equal equal n. Exactly what we did last time. Then see out a star. Or else see out a space. Understood? And outside the for loop, see out end l. Right? That's how it looks. Let me do one thing. Let me decrease the size to 20. I think now you can see it clearly, correct? Yeah. Let me also close this. So, I think now it's more clear. Correct? Now, this will print us the top hollow triangle. Or the top hollow pyramid, if you must say. But also, we must not forget that we have a bottom triangle as well. int i is equal to n minus one i is greater than equal to one i minus minus for int j is equal to one j is less than equal to n minus i j plus plus c out a space for int k is equal to one k is less than equal to two into i minus one k plus plus We'll again have an if statement. If k equal equal one k or k equal equal two into i minus one or i equal equal Okay. This time I forgot we can't write i equal equal n. The reason is when we did this part when we did this part we wrote i equal equal n because we wanted this. We wanted five stars right here. So, i equal equal n means n is number of rows. So, 1 2 3 4 5. So, the moment I reaches 5, we wanted a star. This time we don't have a star in the middle row. So, one place where we messed up was we need to remove I equal equal N from both the places. And now we say see out star else see out space outside the for loop see out and then Understood? Is that clear? Yes or no? Is it clear? Come on. Tell me. We've got a lot more questions, like eight more questions. So, show some enthusiasm. Cuz we are on question number 17 and we've got 25 questions somehow. Yeah. Perfect. Let's compile this. Oh, okay. So, we've messed up somewhere. Let's see. Um da da da da This is one. Da da da da Okay, hold on. Let me just check the code. And I is equal to one, I is less than equal to N I plus plus, J is equal to one. Okay, N minus I. This is where we messed up. Let's check the other code as well. Okay, J is equal to one, two, three, four, five. Okay. This should work. Yes, it does. So, we missed an I minus one in our code. So, it was J less than equal to I minus one and not J less than equal to N. And it worked. That's what our pattern is, right? This is what we wanted. A hollow diamond. Yeah. Cool. Perfect. Moving on. Let's get to the next question. So, has anybody anyone heard of Pascal's triangle? Anyone? Ever heard of Pascal's triangle? One, one, one. One, two, one. One, three, three, one. One, four, six, four, one. Exactly. That's that's the right way of doing it. Cool? You've heard of Pascal's triangle, right? So, the middle number is a sum of the other upper two. So, like one and this this two is a sum of these. This three is a sum of this. This four is a sum of 1 + 3. And so on. Right? So, how Okay, it's it's just a mathematical series basically. Uh Pascal's triangle is what we call it. Okay. And uh the code goes something like this. So, we say Give me a sec. Let me just make some space on my desk. Yeah. And then we say int n equals five. for int i equals zero i is less than n i plus plus And we say Let's let's consider a value. Say int val equals one. And then we're going to be in um No, it's going to be a for loop. for Let me write the code and then I'll explain you what it is. j is less than equals i j plus plus c out val and a space. And then we'll update value. val is equal to val into I minus J by I J plus one. Yeah, that's what the formula is. That's it. See out and L and we're done. What actually happens is weird again, if you remember I equals zero I less than N I plus plus increasing triangle. That's it. So increasing triangle is what we need, but the only difference is the the element that we are printing is not star this time. We've just considered a new variable called well. We'll keep updating the value of well the variable and update and print it at our ease. The formula for the formula for Pascal's triangle would be well equals well into I minus J divided by J plus one. That's the formula. So every time I want to just print this for example right now well is one, okay? In the next time say we are on I equals zero J equals zero. So it's going to be well is equal to well into zero minus zero by zero plus one. And what's zero by one? What's zero by one? Tell me, what is zero by 1. Quick. Right? Let's do one thing. Let me code it first. Cool. And then you'll be able to understand better. Let's code it. New file. Pattern 21 .cpp. Yeah. So, we said int n is equals five as usual. Correct? That's what we did. Right? And then what did we say? For int I is equals zero, I is less than n, I plus plus. And inside the for loop, we said int val is equals one. For int j is equals zero, j is less than equals I, j plus plus. C out val val and a space. And update val saying val is equals val into i minus j by j plus one. Outside the for loop, see out endl. That's it. Uh let's say Let's compile 21. And let's run 21. That's what we wanted. Uh I have a mechanical keyboard. It's called Cherry MX red or Cherry MX blue, I guess. Something like that. Cherry MX blue, brown, red, I don't know. I don't remember what switches I have. Hold on, let me check. Yeah, I've got the Cherry MX red. Yeah. Cuz I use two different keyboards, one for my work and one for my streaming here. So, I don't remember which one I have. home But, yeah. Cool. Let me just take a snip of this code and dry run it for you on the on the notes. Everyone up for a dry run? Yes or no? Quick. Everyone up for dry run of this code? Okay, perfect. See, Harsh has requested it. Cool. Now, look at this very carefully. What we are is n equals 5 i equals 1 j oh i equals 0 j equals 0 val equals 1 Okay? I'm going to print the output here. And I'm going to do the dry run here. Okay? So So what we say is i is equal 0, i is less than 5 because n is 5, i plus plus. 0 is less than 5. We get inside the loop. We say val is equal to 1. j equals 0 0 is obviously less than equal to 0 because look at this i j. Keep looking at these values. 0 0 So 0 is less than equal to 0. That is correct. I get inside the loop. And I print val. Okay? So I print 1. Then I update the value of val. So now val becomes 1 into 0 minus 0 by 0 plus 1 which is 1 into 0 by 1. What would this be? Tell me. Tell me. 1 into 0 by 1. What would it be? 0 Would it be zero? Yes or no? So now val becomes zero. Correct? So we update the value of val to zero. And then we go again. See out end L. Right? We come outside the loop. We come outside the loop and we go J++. J becomes one. Now one is not less than equal to zero. So we go outside the loop and we come to the next line. Okay? So we were in line number one, we go to the next line. And this time we get I++. I becomes one. Now one is still less than five. And obviously the value of val gets updated to one again. So one is less than five, we get inside the loop. Val is equal to one. Again, we go through the same thing. This time J is equal to zero. J is less than equal to I. Okay? Uh J gets updated to zero. Zero is less than equal to one. Very nice. We get inside the loop. Print val. Val is one again. We print val. Now we update val. Val becomes one into I minus J, one minus zero upon J plus one, zero plus one. So one into one by one becomes one. Correct? So val stays one again. Val is one. We come outside. J++, J becomes one. Okay? Now, J becomes one. One is less than equal to one. Again, true. I come inside. And I print val again. So, one and a space. I come out. Okay? Again, J value would be one again because it's it's the same thing. Because it's going to be val equals one into one minus one upon one plus one, which is one into zero by two, which again comes out to be zero. Okay? So, val becomes zero, but doesn't matter because even if it is zero, I'm going to come outside the loop. J plus plus will become two. Now, two less than equal to one, it is again false. So, everything gets reset. Okay? So, we come out again. See out end L becomes true. We go to the next line. I plus plus, I becomes two. Okay? Now, I becomes two. And we go again. Val is equal to one. Now, two is less than five. So, obviously we get entry inside the loop. Okay? Um Now, I is two, J is zero. Zero is less than two. We get entry inside the loop. We print val and a space. Val gets updated. This time value is equal to one into I minus J, two minus zero upon J plus one, zero plus one, which is one into two by one, which is two. So, value gets updated to two. Correct? Okay, not bad. We go outside the loop. J++. J becomes 1. No doubt. This time um J becomes 1. 1 is less than equal to 2, right? 1 is less than 2. We get inside the loop. And we print val, which is 2 now. So, we print 2 and a space. We update val. So, val equals 2 into I minus J, 2 minus 1 upon J plus 1, 1 plus 1, which is 2 into 1 by 2, which is 1 again. So, val becomes 1. Going out again. J++. Let's go. 2 less than equal to 2. 2 is equal to 2. We got the answer. We get inside the loop. We print val, which is 1. So, 1 and a space. Val is updated. Doesn't matter. I won't waste time in updating it because I know when I come out, J will become 3, and 3 is not less than 2. So, we'll obviously end up doing end up. So, let's not waste time in updating val. End up. So, again, val gets reset. J gets reset. And everything gets reset. The only thing which is not resetting is I. I becomes 3. 3 is less than 5. In this situation, we go again. Val Again, 0 is less than 3. We print 1 because we print val. And a space. Update val. Which is value equals 1 into 3 minus 0 upon 0 plus 1, which is 3. So, we update the value of val to 3. Okay, no doubt. Come out. J++. J becomes 1. We print value again. Three and a space. Update value. This time, let's update value again. So, val equals three into I minus J, which is three minus one upon J plus one, which is one plus one. So, it becomes two upon two, which is one again. So, three into one is three. So, the value remains three. No issues. We go out. J plus plus. Okay. J becomes two. Two is less than three. That is right. I get inside the loop. Print val, which is three. So, three space. Update value. Value is equal to three into I minus J, three minus two upon J plus one, which is two plus one, which becomes three into one by three, which is one. So, we update the value of val to be one. Okay. Let's remove this so that we have less confusion. Come outside the loop. J plus plus. Three less than equal to three. Three is equal to three. Correct. Okay. So, we go again inside the loop. Print value. Value is one. One space. And update value. I won't update value because I know it's going to be um the end of the loop. So, J becomes four. Four is not less than three. Everything gets reset. Everything goes reset. J becomes zero. Value becomes one. This all goes off. I plus plus. Four is less than five. So, we'll go again. Value is one. Go inside. Zero is less than four. We get entry inside the loop. Next line. We print val. Val is one. Okay. Let's go down a little bit. Okay? Yeah. Now, let's do it again. Um val is updated. So, val becomes one into I minus J, which is 4 minus 0, and J plus 1. 1 plus uh 0 plus 1, which is 1 into 4. So, val becomes 4. Okay? So, we go again. J plus plus, J becomes 1. 1 is less than 4, we'll print val very quick. We'll print 4 and a space. And update val. So, val is equal to 4 into I minus J, which is 4 minus 1 upon J plus 1, which is 1 plus 1. So, 4 into 3 by 2, which is 6. 2 into 3 is 6. Val becomes 6. We'll remove this. Come out. J plus plus. 2 is less than 4, we'll print 6 and a space. And update val. Val is equal to 6 into I minus J, which is 4 minus 2 upon J plus 1, which is 2 plus 1, which becomes 6 into 2 by 3. 2 into 2, which is 4. So, val gets updated to 4. Reset again. Come out. J plus plus, J becomes 3. 3 is less than 4. 3 is less than equal to 4, because I is 4, J is 3. We get entry inside the loop again. We print val, which is 4 and a space. Um val updated again. Val is equal to 4 into I minus J, 4 minus 3 upon J plus 1, 3 plus 1, which is 4 into 1 by 4, which becomes 1. So, val becomes 1. We go again. J plus plus. 4 is less than equal to 4. 4 is equal to 4. We get the entry again. Val is equal to 1. So, I'll print 1 and a space. I won't update Val because I know the loop is going to end. I come out. J plus plus. J is 5. 5 is not less than 4. Everything goes for a reset. This goes reset. This goes reset. This goes reset. And J is equal to 0. Value is equal to 1. I plus plus. And we are on the last line. Now, 5 is not less than 5. Correct? Hence, this becomes our output. 1 2 3 4 5 five lines of code uh five lines of output. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Understood? So, this was the dry run of Pascal's triangle. And that is how the code works. Is it clear to everyone? Yes or no? Come on. Yes or no? Tell me. Understood? Damn, that was a long dry run, man. That was a long long dry run. Uh how to build thinking power for pattern making? I solve approx 50 problem, but you have to keep solving. 50 If you're not done in 50, go for 60. Go for 100 problems, but keep on going. The more patterns you solve, the better you're going to be. Understood? Let's go to the next question. Are you guys up for the next question, which is binary triangle? So, what do you think? How how can we solve the binary triangle? Very simple. Look. Okay? Just two simple loops for int I is equal to 1 I is less than equals n I plus plus for int I is J is equals 1 J is less than equals I J plus plus And inside the loop, just write see out I plus J percentage two. That's it. Done. See out end L. I'll code this up and I'll dry run it for you. You'll understand how it works. Same pattern but we change the value and the formula. Yes, exactly. We change the value and we change the formula. And this time the formula directly comes in your C out. I plus J percentage two and a space. Understood? Let's do it. Pattern 22 it is. Right? 010010 blah blah blah Understood? Is that what we needed? binary triangle My solution in my head is just to print Okay. How you can start it with one, you can start it with the zero. That's your call. You want to dry run this? Dry run this code? If what what will you do if you want to start it with the one? Let's try. Let's try it. Um Brainstorm this. What would you do if you want to start it with one? Hm. Good question. Yeah, it it should start with zero. Because you're starting binary, it should be zero one zero one. Let's dry run this. Anybody up for a dry run? Yes or no? Quick. Who's up for the dry run? Who wants to understand how it works? Tell me. Anyone up for the dry run? Okay, let's do it. Look. So, this is how the program works. You have n equals five. Okay? So, you say let's write five here. One is less than five, you get entry in the loop. Let's say I equals one. J equals one. Okay? One is less than five, you get entry in the loop. J is less than equal to I. One is less than equal to I, you get entry in the loop. See out I plus J percentage two. Two percentage two is zero because you don't get a remainder if you divide two and two. Hence, in the output you print zero. You come out. J plus plus. Two is not less than one. Hence you go to the next line. J gets reset. And we start over again. I plus plus. Two is less than equal to five. You get entry in the loop. One J is equal to one. One should be less than two, which is true. You get entry in the loop. And this time you print two plus one percentage 2, which is 1. Right? 3 by 3 by 2 will give you the remainder as 1. So, 2 percent 3 percentage 2 would be 1. Hence, you print 1. You come out of the loop. J plus plus. 2 is equal to 2. You get entry in the loop again. 2 + 2 percentage 2, which is 4 by 2, will leave you 0 as a remainder. So, you print 0. Come outside the loop. J plus plus. 3 is not less than 2. Hence, you go for a reset. You come outside this. End L gets printed. I plus plus. J goes back to 1. 3 is less than 5. Hence, we get in the loop again for the third time. Um 1 is less than 3. So, we go for a print, which is 1 + 3 percentage 2. 4 percentage 2 will give you 0. Hence, you will go for a J plus plus. 2 is less than 3. So, you get inside the loop again. 2 + 3 percentage 2 will give you a remainder as 1 because 5 by 2 would be 1. 5 by 2 will give you a remainder 1. Coming out of the loop again. J plus plus. 3 is equal to 3. You get in the loop again. 3 + 3 by 2, which is 6 by 2 will give you a remainder as 0 again. Coming outside the loop. J plus plus. 4 is not less than 3. Hence, we'll go for an end L and an I plus plus. I becomes 4. We go for a hard reset. All of this goes off. And we go again. 4 is less than 5. We get inside the loop. 1, which is J, should be less than equal to 4. So, 1 is less than equal to 4. We go for a C out, which is 1 + 4 percentage 2. 5 percentage 2 would be 1. We go for a J++. 2 + 4 2 is less than 4, so we get inside the loop. 2 + 4 % 2 which is 6 % 2 will give you 0 because 6 by 2 is 3 and it will give you 0 as a remainder. Again, J++. 3 is still less than 4. 3 + 4 % 2 which is 7 % 2 will give you 1 because 7 is odd. J++ again, 4. 4 is less than equal to 4. You get inside the loop again. 4 + 4 % 2 would give you 0 again because 8 % 2 will give you a 0 remainder. Get outside the loop again, J++. 5 is not less than 4. Hence, the loop ends here. You go for a hard reset. All of this goes off. This goes off. J becomes 1. I++ and end L. So, you go in the next line. 5 is less than equal to 5. We go for another iteration. Get inside the loop. J should be less than equal to I. 1 is less than 5. Agreed. We go inside the loop. 1 + 5 % 2 6 % 2 will give me 0. J++. 2 is less than 5. 2 + 5 % 2 would give me 1. J++. 3 is still less than 5. 3 + 5 % 2 would give me 8 % 2 which is 0 because 8 is even. J++. 4 is still less than 5. 4 + 5 % 2 which is 9 % 2 would give me 1. J++. 5 is less than equal to 5 which is 5 is equal to 5 is true again. So, 5 + 5 % 2 would give me 0 again because 10 % 2 is 0. J++. 6 is not less than 5. Hence, we go for a hard reset. J becomes 1. All of this goes off. And we go for end L which is the next line. I++. Now, 6 is not less than equal to five. Hence, this loop gets terminated and this is our final output. Which is a binary binary binary binary triangle. Understood? Yes or no? Did you guys understand the dry run? You just have to play around with the code. For some reason, it doesn't Okay. Next question. Up for another one? If it's interesting, we'll go for another question. Come on. You guys excited? Yes or no? Write a yes or no in the chat, quick. Yep. Understood? Try to repeat and move Okay. Yeah. It does. Let's go for another program. Which is this. Everybody knows what a right-angled triangle is, correct? Right-angled triangle. This is a right-angled triangle. What if we want to make a hollow right-angled triangle? Tell me. If you want to make a hollow right-angled triangle, what are we going to do? Have a good one. Have a good one, Mark. Bye-bye. Below. What are we going to do? Any guesses? See, it's going to be something like this. In n is equals 5 again. Yeah, you need to have an if else. And then it's going to be four. int i equals 1 I less than equals n I plus plus for int J equals 1 J less than equals I J plus plus And here it's going to be if J equal equal 1 or I equal equal 1 Oh, no. Uh if J equal equal 1 or J equal equal I or I equal equal n. That's when you print a star. Otherwise print a space and then Let's code this up and that's when you'll understand. And then we'll dry run it. Then we'll dry run it as always. Correct. else Uh see out space. Understood? Let's try running this. pattern 23 That's it. a hollow triangle or a hollow right-angled triangle is what we get. Understood? I'll I'll explain. Let's understand the logic behind it. Look. What is happening here? n = 5 i = 1 j = 1 Now understand this thing. Okay? If we see j represents columns i represents rows. So j b J 1 K Barabara here. First first column may want to be home. Print career. Stop. Then. Second row K. Second column may I'm starting career. Third row K third column may I'm starting career. Fourth row K fourth column may I'm starting career. Fifth row K fifth column may I'm starting career. Which means when row and column are equal, which is the better to get ahead J other one K Barabara here to I'm starting career. J other I get better here. 2 is equal to 2 to start. 3 is equal to 3 to start. 4 is equal to 4 to start. And 5 is equal to 5 to start. And the last thing is J B I number of rows K total number of rows K Barabara which is 5. And 5 here to fifth row maybe sorry to start playing to win. Understood? That is why we say that if J is equal equal 1 J equal equal I and I equal equal N print a star else print a space. In sub J okay a lava J B up go print career print a space. Understood? Yes or no? Some job is shake. No yeah, I won't be teaching Python. I won't be teaching Python. Okay. I'll have water. And we'll start. Let's do a dry run for this. Okay? Let's do a dry run. I is equal to one. I should be less than equal to n. Now, what I'm saying is one is less than equal to five, correct? So, I'm going to get an entry inside the loop. Right? I get entry inside the loop. J is equal to one. And it should be less than equal to I. I is one. One less than equal to one is true. I get an entry inside the loop. Now, what did we say? If J is equal equal one, J is equal equal I, I is equal equal n. Any of these is true, so print a star. Now, J is equal equal to one, true. So, we'll print a star. Uh let's print a star here. Okay? Hello. Now, we come outside the loop. J plus plus, J becomes two. Now, two is not less than one, correct? Two is not less than one. So, we'll get outside the loop. When we get outside the loop, we go through end n. So, we go to the next line. Okay? One. Let me just Okay? We go to the next line. And we have I plus plus. This becomes one again. Now, one Now, first of all, I less than equal to n. 2 is less than 5, true. I get inside the loop for the second time. j is equal to 1. j should be less than equal to i. 1 should be less than 2. Agreed. We come inside the loop. If j is 1, which is true, we print the star. And we come outside the loop. j plus plus. 2 less than equal to 2. Correct? Very good. I get inside the loop again. j is equal equal 1. Not true. j equal equal i. Both are same. Correct? We print the star. Come outside the loop. j plus plus. Now, 3 is not less than 2. j is 3, i is 2. Both are not equal. Well, like not equal and j is not less. Hence, we come outside the loop. We have end l, which means we go to the next line. And we have i plus plus, which means i becomes 3. This goes back to 1. Okay? I'll tell you what. This gets reset. I get into the loop third time. How? 3 is less than 5. I get into the loop for the third time. Okay? Now, j is equal to 1. 1 is less than 3. I get in the loop. j is equal to 1, which is true. I print the star. I come out. j plus plus. This time, 2 is less than 3. I come in the loop. j equal equal 1. Not true. j equal equal i. Not true. i equal equal n. Not true, because 3 is not equal to 5. Hence, we don't get to print the star. We print a space. So, we print the space here. Come outside the loop. j plus plus. 3 is less than equal to three? Correct. I get inside the loop. J equal equal one? Not true. J equal equal I? True. I print a star. Come outside the loop. J plus plus. Four is not less than three. Hence, this loop gets terminated. End L. I go to the next line. And I plus plus becomes four. This gets reset to one. Okay? And this all goes for a reset. Again, one is less than four, which is correct. I get in the loop. J equal equal one, which is correct. I print a star. I come outside the loop. J plus plus. Okay? Huh, will be considered. Okay? What's the word? I say J plus plus. Two is less than four. I get in the loop. J equal equal one? Not true. J equal equal I? Not true. I equal equal N? Not true. Which means I'll print a space. J plus plus, cuz I'm coming out the loop. Three less than four? True. Coming inside the loop. J equal equal one? Not true. J equal equal I? Both are not equal. Not true. And I equal equal N? Four is not five. Not true. So, I print a space again, because I go for else. Come outside the loop. J plus plus. Again, four is less than equal to four. True. I come inside. J equals one? Nope. J equals I? True. So, I print a star. Come outside the loop. J plus plus. Five is not less than equal to four. Loop gets terminated. We go for an end L, next line. All of this gets reset. Okay? 1 2 3 4. Yeah. All of this gets reset and we go for the I plus plus. Five is less than equal to five. That is true. We get inside the loop. Okay? Now, one less than five, true. I come inside. J equal equal one is also true and N equal I is also true. So, we print the star. We come out. J plus plus. Two is less than five. We get in the loop. J equal equal one, not true. J equal equal I, also not true. But, I equals N is right because I is five now. So, we print a star. Okay? Come outside the loop. J plus plus. Three less than five is true. I come inside. J equals one, nah. J equals I, no. But, I equals N, true. I print the star again. I come outside. J plus plus, which is four. Four is less than five is true. I come inside. J is one, no. J is I, no. But, I is equal to N is true. I print the star again. I come outside the loop. J plus plus. Five is less than equal to five. Again, true. I come outside. Come in the loop. J is equal to one, no. But, J is equal to I, true. We print the star again. Come out. Six. Nope. Because six is not less than five. And then finally, six is not less than five. So, finally the final pattern that you get in a hollow triangle after all the iteration would be this. Understood? Yes or no? So much I got. I'm not in India, so many of you are not near. Many of you are missing the 7:00 p.m. here. Someday. Any doubts, anyone? Any doubts? How many do you want to download next question in the middle of the ticket? Hello, you got question going in here still free answer to get in the middle. Ticket. Do question go to the zoo. Do we have question going in here? How do you see sheet cursor to get to me DSA after DSA sheet color DSA sheet marker now get me DSA near to me after I get to me DSA near to me after to play DSA for the free sheet color. Ticket. I don't need question going to be a dancer repeating numbers question going to be a dancer. 1 2 2 3 3 3 4 4 4 4 or 5 5 5 5 5 Get to me Joe has me winning number to get me by print over first home and one a bar over second home and two double print over third home and three double print over fourth home and four four double print over fifth home and five five double print over. And I might as well question. And I'm a good question. It's a good enough simple or it's a not sure that I get them back to us to get away care. You have a good question. They go. Both simple, both simple answer over here. I mean we can get them out. int n is equal to five minute. four int I is equal to one. I is less than equal to n. I plus plus. four It's kind of four. int J is equal to one. J is less than equal to I. J plus plus. See out. I space That's it. See out. And then I'll get a question cut them. That is it. Cut them that up by by. You can have us. I would get it to go to some other day. Okay. Do you have any other than to play a bit of DSA code? You can about sheets to get a good sheet cut near to get about. I 11:35 now how to be or 10 minute 10 minute make up and get there. A a question a question or getting it. I'll forget them getting it. Or we doubt or we doubt of a check. To go down. Turn over here and I have a Guess how how how is the temperature and weather in India without guess how guess how much of them look at India me. But you have to learn it. Getting over here guess how to learn it. Go Tell us how much we are getting more than what I can work on question getting it to get back. May I have a b c block from nearly that classes getting over here going to see going to see this Abhishek or We are going to see how going to see how to see this too. You have to eat that delete or download stream to go to the ticket by the message I like to go to look at the to my message to my bus delete stream that you have any. Return my company can be a second order bill could be by. Bill could be near after me to come bill could be near after me to come bill could be near after me to come bill could be near after me to come bill could be near. I work as a senior applications analyst in London. a a pinch of DSA Sorry, DSA may I need to go to my extreme pain. Just see how to do it. My company with the bill could use me over there. I don't know how much fun I had with my mother. So, I want to ask you a question again. What about the way? Do we have to get out of here? How I interview may he come out of it? What do you want to know about me? Do we have any question here and I will see you at the old version of the latest They can get out of here. Take care. What should we do with this man? What is the same code here? I need to bring to go to the same way. But it bought you to see this change coming. Yeah. It bought you to see this change I guess man. What could I say? Before in I is equal to one. I is less than equal to n. I is greater than I is less I is equal to n I is greater than equal to one or that I get to get it. I minus minus. or before in J is equal to one. J is less than equal to I. J plus plus see out I. That's it. I will see out and then look at the bar. But I work for the UOL in London. Or Go to the hashtag include. I will stream. Using namespace STD. int main Okay. int n is equal to 5 for int i is equal to n i is greater than equal to 1 i minus minus for int j is equal to 1 j less than equal to i j plus plus uh see out i space see out end l That is all. Oh, yeah. Run the code as cool. Easy peasy lemon squeezy. 25 number code here. I would have to save it in here. Yeah. I would have to go. That's it. I would have to buy them for the army. So, I would have to go. That was the code. That was the code. Oh my god. the Uni of London Any doubts, anyone? Abhishek Okay. My bad. My bad. My bad. Any doubts, anyone? Perfect. code That's how That's how it is. You can't help it. Huh? So, thank you so much metro for watching. I will see you tomorrow same time 10:00.

Original Description

In this video, we explore one of the most important building blocks in programming – Functions. Learn how functions help in writing clean, reusable, and efficient code. We cover the basics, including function definition, declaration, calling, parameters, return types, and real-world examples to help you grasp the concept clearly. Whether you're a beginner or revising for exams like GATE, placements, or coding interviews, this session will solidify your understanding of functions in programming. Get all important links here: 🔗 Visit website: https://geeksforgeeks.org/ Explore Premium LIVE, Online & Offline Courses : https://geeksforgeeks.org/courses/ Solve POTD: https://www.geeksforgeeks.org/problem-of-the-day Ongoing contests, hackathons and events: https://www.geeksforgeeks.org/events Follow us for more fun, knowledge and resources, join us on our social handles: 📱Take GeeksforGeeks everywhere in your pockets! Don't forget to download our official app: https://geeksforgeeksapp.page.link/gfg-app 💬 X- https://x.com/geeksforgeeks 🧑‍💼 LinkedIn- https://www.linkedin.com/company/geeksforgeeks 📷 Instagram- https://www.instagram.com/geeks_for_geeks/?hl=en 💌 Telegram- https://t.me/s/geeksforgeeks_official 📌 Pinterest: https://in.pinterest.com/geeks_for_geeks/ Also, Subscribe if you haven't already! :) #ProgrammingBasics #FunctionsInC #FunctionTutorial #FundamentalsOfProgramming #CodingForBeginners #LearnToCode #GATE2026 #PlacementPrep #ComputerScience #TechEducation #CodeWithMe #CProgramming #ProgrammingConcepts #SoftwareDevelopment #GeeksforGeeks #Learntocode #GfG
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from GeeksforGeeks · GeeksforGeeks · 0 of 60

← Previous Next →
1 How I got into Walmart | Shailesh Sharma
How I got into Walmart | Shailesh Sharma
GeeksforGeeks
2 Upgrade yourself In 29 Days | GeeksforGeeks
Upgrade yourself In 29 Days | GeeksforGeeks
GeeksforGeeks
3 Learn AWS Fundamentals For Free
Learn AWS Fundamentals For Free
GeeksforGeeks
4 Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
GeeksforGeeks
5 Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
6 Interview Prep Strategies | PayPal
Interview Prep Strategies | PayPal
GeeksforGeeks
7 OLX Interview Preparation Strategies | Hukam Singh
OLX Interview Preparation Strategies | Hukam Singh
GeeksforGeeks
8 Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
9 Live Mock DSA
Live Mock DSA
GeeksforGeeks
10 Microsoft Azure For Absolute Beginners
Microsoft Azure For Absolute Beginners
GeeksforGeeks
11 Python for Data Science | Data Science Master Bootcamp | Arpit Jain
Python for Data Science | Data Science Master Bootcamp | Arpit Jain
GeeksforGeeks
12 Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
13 How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
GeeksforGeeks
14 Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
15 TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
16 How I Secured AIR 9 in GATE'22 |  Tushar
How I Secured AIR 9 in GATE'22 | Tushar
GeeksforGeeks
17 Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
18 How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
19 Learn Data Structures and Algorithms | GeeksforGeeks
Learn Data Structures and Algorithms | GeeksforGeeks
GeeksforGeeks
20 Interview experience at Flipkart | GeeksforGeeks
Interview experience at Flipkart | GeeksforGeeks
GeeksforGeeks
21 Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
GeeksforGeeks
22 Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
23 Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
GeeksforGeeks
24 Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
25 Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
26 Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
27 Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
28 Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
GeeksforGeeks
29 How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
30 Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
GeeksforGeeks
31 How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
32 Journey from Tier 3 to JusPay | GeeksforGeeks
Journey from Tier 3 to JusPay | GeeksforGeeks
GeeksforGeeks
33 Geeks Summer Carnival 2022 | GeeksforGeeks
Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
34 Dispelling Myths and Pre conceptions of Programming Languages
Dispelling Myths and Pre conceptions of Programming Languages
GeeksforGeeks
35 Must Do System Design Questions
Must Do System Design Questions
GeeksforGeeks
36 Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
GeeksforGeeks
37 Get Hired at NEC | Job-A-Thon 8
Get Hired at NEC | Job-A-Thon 8
GeeksforGeeks
38 Journey from Tier 3 college to Microsoft | GeeksforGeeks
Journey from Tier 3 college to Microsoft | GeeksforGeeks
GeeksforGeeks
39 Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
GeeksforGeeks
40 GeeksforGeeks: Redesigned
GeeksforGeeks: Redesigned
GeeksforGeeks
41 From Tier 3 to cracking multiple interviews | GeeksforGeeks
From Tier 3 to cracking multiple interviews | GeeksforGeeks
GeeksforGeeks
42 Live Mock DSA
Live Mock DSA
GeeksforGeeks
43 Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
GeeksforGeeks
44 DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
GeeksforGeeks
45 GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GeeksforGeeks
46 Journey from JIIT to Adobe
Journey from JIIT to Adobe
GeeksforGeeks
47 Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
GeeksforGeeks
48 Interview Experience at Google | Tech Dose
Interview Experience at Google | Tech Dose
GeeksforGeeks
49 Live Mock DSA
Live Mock DSA
GeeksforGeeks
50 Interview Experience @ Amazon | GeeksforGeeks
Interview Experience @ Amazon | GeeksforGeeks
GeeksforGeeks
51 My journey through the tech world from India to US | Vidushi | GeeksforGeeks
My journey through the tech world from India to US | Vidushi | GeeksforGeeks
GeeksforGeeks
52 Complete Interview Preparation Course | GeeksforGeeks
Complete Interview Preparation Course | GeeksforGeeks
GeeksforGeeks
53 Live Mock DSA
Live Mock DSA
GeeksforGeeks
54 Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
GeeksforGeeks
55 GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GeeksforGeeks
56 How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
GeeksforGeeks
57 LINUX for Beginners | GFG x Itversity
LINUX for Beginners | GFG x Itversity
GeeksforGeeks
58 My interview experience at Walmart | GeeksforGeeks
My interview experience at Walmart | GeeksforGeeks
GeeksforGeeks
59 Get Hired at Speckyfox
Get Hired at Speckyfox
GeeksforGeeks
60 Live Mock DSA
Live Mock DSA
GeeksforGeeks

Related Reads

Up next
Exploring NotebookLM in Unexpected Ways 🤯 | Hidden AI Use Cases You Should Try
Pavithra’s Podcast
Watch →