 Hello, everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we are having day 11 of this CNC++ coding bootcamp. And in today's session, I will be discussing about while loop. So yesterday, like in previous session, I discussed about for loop, how we can write for loop, what is the syntax, and how we can implement the programs. So today we'll be exploring while loop and I will be explaining you the syntax as well as few examples we'll be implementing. Okay, so once again, welcome everybody, those who are live. So I welcome them and those who will be watching the recording. So I appreciate your time as well, like you are learning day by day, the basics of coding, right? So moving forward, if you want to know about me, so you can go through with this slide. So this slide explains everything about me, what I'm doing, how many years of experience I have and everything. So just go through with this slide if you know me and if you are continue if you are following other bootcamps as well. So I think you already know things about me. And I in every session, I just remind this phrase like consistency is the key. So please do regular practice so that you can become expert in whatever you are doing. And you can scan this QR code and you can become part of Telegram group where folks who are learning C and C++, they are connected, right? So follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram. And all the important links are available in the video's description. So before starting this while loop concept, I just want to say this bootcamp is basically for non technical folks who are not knowing programming. And for BTEC students, those who are starting their programming journey. So these two audience are main target and other than that, if anybody wants to learn basics of coding with the help of C and C++ programming languages, so for them also, this bootcamp will be beneficial. Okay, so in yesterday's session, I explained you like how we can implement for loop. So in today's session, we'll be learning the implementation of while loop. Okay, so first of all, we need to understand how while loop we need to write, right? So what is the syntax of this while loop? So here you can see, we have all three ingredients that we discussed in for loop. So in for loop, I told you like we have three things. First is initialization, second is termination condition and third is increment and decrement. So both the things, sorry, all three things are available here with the syntax as well. So if you see the syntax, so here, we have initialization then with while we have condition and inside curly basis, we need to write statements. And then we can write increment and decrement, right? So basically initialization means beginning and condition means like how many times you want to execute your loop. So condition decides like when your loop will be terminated. And whatever statements you want to repeat, you can write inside curly basis. And with the help of increment and decrement, you can like if you have initialized your loop from lower value as the condition, so you will be incrementing your loop. And if you have initialized your loop with higher value as the condition, so in that case, you will do decrement. Okay, so I am going to explain you the syntax with the help of an example. So in yesterday's class, I implemented one problem that that was related to printing natural numbers, right? So I will be converting that for loop into a while loop. So here you can see we have a loop. So I'm just going to modify this. So here we have two variables, I and N, then we need to read a value from user in N. So basically, I am going to print all the natural numbers. So I am going to initialize I with one, then I need to write while I less than equals to N. So this is the condition. And inside while loop, first I will be writing printf. So printf will display value of I and then I can write I plus plus. So this way I can implement while loop. So this is initialization. Then in while you have the condition. And inside while we have increment, right? So this way while loop will be implemented in C programming. So all three things are available here initialization condition and increment decrement. So right now here loop is starting from one and it will execute till N. So for example, N is 50. So this loop will be executing 50 times. So all the 50 natural numbers will be displayed on screen. So now what I'm going to do, I'm just going to click on run so that I can show you the results. So it started execution. Now here I'm entering 10. So you will see 10 natural numbers on output screen. So output is starting from one and then one, two, three, four, five, six, seven, eight, nine, 10. So till 10 results are displayed. So this way you can implement while loop in C programming and this loop is used in other programming languages as well. Now if you want to display natural numbers and decreasing order, so that is also possible. So here you can write I equals to N. Then you can write I greater than equals to one. So this way loop will start from N. So if N is 10, so value of I will become 10, then here condition will be I greater than equals to one because we need to like if N is 10, so we need to go till one so that loop will be terminated by printing all the values between 10 to one. And here we need to write minus minus. So this time it is basically decrement. So initialization is basically from N condition is I greater than equals to one. So if number is greater than equals to one, then only it will be displayed. And every time I will be decremented by one. So this way you can implement while loop in decremented order. So now if I execute this code and if I enter 10, so you can see natural numbers are displayed in reverse order. Okay, so I hope with this way you are able to understand how we can write while loop in C programming. Okay, so moving forward now, I have lots of examples for you that I'm going to implement. So first problem says calculate some of individual digits of a number. So if you remember in the beginning, we solved some problems where we were having two digit number, three digit number, four digit number, and we used to calculate some of those numbers. Some of those numbers means some of digits of those numbers, right? Now, what is the requirement? We can have number which is having any number of digits. So your number can have two digits, it can have three digits, it can have four digits, it can have five digits. And we don't know the number of digits those are available in the number, right? So this first problem I'm going to solve with the help of loop. So we need to calculate some of individual digits of a number and number can have any number of digits. So here initial part will be same like we need to receive a number from user. Now here I'm writing condition like while n is greater than zero. If n is greater than zero, then we need to write certain steps here. Now what I'm going to do? So here you need to understand how we build the logic. Okay, so for example, n is 123. So right now n is 123. So what I'm going to do, I'm going to have a variable a and what I will do, I will split this number into three parts. Three will be separate out, then two will be separate out and then one will be separate out. So what I'm going to do, I'm writing a equals to n modulus 10. So right now value of n is 123. Value of n is 123. Okay, so if we have number as 123, and if we divide this number by 10, so we will be having remainder value and this time remainder will be three because if you divide 123 by three, sorry, if you divide 123 by 10, so remainder will be three. Okay, so that three will be assigned into a. So the value of a will be three. So this way, with this loop like in the first step will behave will be having the last digit. So last digit will be stored inside a. Now what I can do, I can remove this three from n. So this I will do through this statement. So if you divide any number by 10, so last digit will be removed. So if you divide 123 by 10, so result will be 12. Actually it will be 12.3. But in case of integer data type, 0.3 will not be considered and it will be removed automatically. So with this statement n will be 12. And now I can write s equals to or maybe sum equals to sum plus a sum equals to sum plus a and here I'm writing sum equals to zero and I is not required. So I'm just removing that variable. So initially sum is zero. So right now value of sum will be three because what we are doing we are adding a into the sum variable, right? So this way what is happening the value of a will be added into some and if it will be stored into some variable itself. Now this is the first iteration of this loop. Now we need to check this while condition once again. So right now n is 12. Right now n is 12. Okay, so 12 will be divided by sorry 12 will be divided by 10 to have modulus value. So now we can say n is 12, 3 is removed. So this time modulus will give you remainder as 2. So a will have 2. Then what will happen if you divide 12 by 10. So right now n is 12. So if you divide it by 10, so n will have one. And now we need to calculate this. So last value of sum is three. Now we need to add a is two. So three plus two will be five. So new value of sum will be five. So this is the second rotation of the loop. Now if we check the condition n greater than zero. So you can see right now n is one. Right now n is one. So one is greater than zero. So again one more time this loop will execute. So n is one. So one modulus 10. If you divide any number that is like lesser by the number with whom you are dividing that number. So one modulus 10. One is not divisible. So remainder will be one itself. So a will be having one. Then we need to solve this n equals to n by 10. So n is one. One divided by 10. So it will be zero. It will be zero because the result will be zero point something. But variables are of integer type. So n will be having zero. And sum is having five. So if we add a is one right now. So if we add one into some so it will become six. Right. And next time if we check value of n. So n has become zero. So this condition will be false because n is zero. So now this loop won't be executed. This is terminated. So this loop basically executed three times because we were having three digit number. If you have four digit number so this loop will repeat four times automatically. Okay. So now after completion of this loop what you can do you can have a printf and here you can write sum equals to percent d and the variable sum you can display. So whatever digits you are having in the number those digits will be added and the complete result will be displayed with the help of sum. So this way the problem was calculate some of individual digits of a number. So this is the basically solution of that problem. Now what I'm going to do I'm just going to click on run so that it will start its execution. So I'm entering 123 and you can see the sum is six. Now again I'm going to run this code and this time I'm going to enter 1234 and you can see some is 10 because one plus two plus three plus four is equals to 10. So whatever number of digits you are entering this code will be working fine for that particular number. So this way like with the help of for loop we are able to implement this and these kinds of problems will be difficult for for loop because in for loop we need to write initialization termination condition and increment decrement in the first line in the header of the loop. But in case of while loop we can place them at different positions. Right. So in in loop header if you want to write only condition and rest of the things you need to write anywhere in the loop. So in that case instead of for loop you can use while loop. Right. So this way I hope you are able to understand how this problem is solved with the help of while loop. Now we have one more requirement which says write a program to read a number and print its reverse. So it is very easy and what we need to do. Suppose number is 123. So we need to print 321 and here also like number can be having any number of digits. So again I am going to use this code and I will be changing this loop little bit. So this line number 13 I will be modifying and it will be printing reverse of the number. So what I am going to write some into 10 plus a some into 10 plus a right. And let's take a different number 247. Now number is 247 and we need to check whether its reverse will be printed or not with the help of this code. So initially you can see number is greater than zero. Number is 247. So it is greater than zero. Then we need to calculate remainder. So 247 modulus 10. So remainder will be 7. So initial value of a will be 7. Then we need to divide n by 10. So n will become 24. Then we need to calculate this. So initially some is 0. So 0 into 10 0 plus a a is 7. So initially some will be 7. Initially some will be 7. So this is the first rotation of the loop first execution cycle of the loop. Now again this while condition will be tested and greater than zero. So right now current value of n is 24. So 24 is greater than zero. It means loop condition is true. So now this modulus operation will be performed. So 24 modulus 10 remainder will be 4. Right now n is 24. So 24 modulus 10 remainder will be 4. Then 24 divided by 10. If you perform this operation so it will be 2. And here initial value of sum is 7. 7 into 10 70 plus a a is 4. So what it will become it will become 74. So this is the second execution cycle of the loop. Now n is 2. So again check the condition n is greater than equals to 0. It is true. Now 2 modulus 10. So again number is less with the deviser. So here you will be having 2 as a remainder. Then if you divide 2 by 10 so the result will be 0. If you divide 2 by 10 so result will be 0. Now initial right now value of sum is 74. So 74 into 10 is 740 and a is 2. So it will become 742. And next time when n greater than 0 will be checked. So n is 0. So this condition will be false and loop will be terminated. And if you print some so the value of sum is 742 which is basically reverse of 247. So this way if you modify this logic a little bit so you will be able to print the reverse of that particular number. Now for validation we need to run this code. So I am going to enter 247. So here you can see the result is 7482. And this code will also work for any number of digits. So if I enter four digit number 1 2 3 6. So you will see the reverse 6 3 2 1. So I entered three digit number then also loop was working fine. If I entered four digit number then also loop was working fine. So this way in any number of digits this code is basically printing reverse of the number. So I hope with this explanation you are able to understand both the solutions first as well as second. Now I am going to focus on third problem. Third problem says write a program to read a number and check whether it is palindrome or not. So basically palindrome means if you have a number and if you reverse that number so the original number and the reverse of that number if both are equal then that number is basically known as palindrome right. So if you reverse the original number and both are same then that number is known as palindrome number. So 121 is palindrome 125 is not palindrome right. So now we have the code of reverse initially number was 247 and here we have reversing some variable. So what I need to do I will be writing if n double equals to some so I am modifying it little bit so if n double equals to some so n we are considering as original number some is having the reverse. So you can say number is palindrome if it is not equal then you can say number is not palindrome right. So we are comparing n with some so n is original number and some is your reverse but here one thing we need to note after completion of this loop value of n will become zero then only this loop will be terminated. Okay I am repeating again after completion of this loop n will become zero always so this condition line number 16 will be false always. So what we need to do before starting the loop we can store the value of n into a temporary variable. So the name of variable is temp a short form of temporary and I am assigning value of n into temp so I am just creating a copy of n into temp and here in this while loop I am just checking the conditions. So we know like after completion of while loop n will become zero so instead of using n we will be using temp here right because temp will be having that original value as a copy now if n will become zero so temp will remain as is temp will be containing the original value of n so here we will be comparing temp with some and if both are equal so temp is basically original number and some is the reverse of that number if both are same so you can say number is palindrome otherwise you can say number is not palindrome now I am going to run this code so that you can understand the logic so I am entering 123 so you can see the output is number is not palindrome and if I run this code one more time and if I enter 121 so you will see number is palindrome so if the entered number reverses equals to the original number then only that number is considered as palindrome okay so if you have any doubt you can ask I hope you are able to understand the logic and we have one more problem that I need to solve so here the problem says we need to read the number and we need to check whether it is Armstrong or not so what are Armstrong numbers so basically if you have a number and if you calculate cubes of individual digits if you calculate cubes of individual digits and if you add those results and if the result is similar to your original number then that number is known as Armstrong so we have a number that is Armstrong which is 153 so if you calculate cubes of 153 so one cube is 1 plus five cube is 125 and then 327 now if you add this so it will become 153 so one cube plus five cube plus three cube if you add the result so if the resultant value is equals to original number then that number will be considered as Armstrong number right so now I'm going to modify this code so that we can have that solution so what I need to do here I need to perform this operation okay so here what is happening sum plus a into a into a so a will be multiplied three times so its cube will be calculated and that cube result will be added every time into sum so now I'm going to solve it for you so initially like number is 153 so this condition is true and greater than zero so n modulus 10 153 modulus 10 so a will be having three then if we go further so n will be having one five three will be removed and if we solve this so sum is initially zero sum is zero initially now zero plus a cube so a is basically three three into three into three is 27 so this is the first rotation of the loop now n is 15 so 15 is greater than zero so this condition will be true so 15 modulus 10 remainder will be five and if you divide 15 by 10 so result will be one and now if you calculate this so sum is 27 already a is five so five into five into five it is 125 plus 27 so 27 plus 125 equals to 152 so this will be the resultant value into sum now one more time this loop will execute because n is still greater than one sorry n is still greater than zero the current value of n is one so one is greater than zero so one modulus 10 remainder will be one one divided by 10 result will be zero and sum is already 152 and a is one so one cube is one so if we add one into this so result will be 153 and next time if you check the condition so n greater than zero so right now current value of n is zero so this condition will become false and here you can see we already assigned n into temp because we know after completion of loop n will become zero so its copy will be available into temp so here we need to compare temp with sum so if both are equal so you can say number is Armstrong and if it is false so you can say number is not Armstrong right so this way I implemented the code now I am going to execute it so initially I am entering 153 so here you can see result is number is Armstrong and if I run it again and enter 123 so you will see number is not Armstrong right so this way I implemented the solution for you for all the programs which I planned for today's session and I hope with this session you are able to understand how we can build the logic how we can use why loop because these problems are difficult and my request would be whatever problem I discussed please implement those problems yourself so that you can also become expert in programming right so whether you are watching the recording so just just watch everything and do practice as well because all these sessions I am doing for your betterment so that you can learn coding easily so daily I am solving few problems explaining them and I would request like day by day if you go through the sessions you will be able to understand the programming very well okay so this is all I prepared for today's session and if you have any question so I know like most of the people watch the recording so if you have any problems so you can reach out to telegram group and you can ask the questions and you can share a review or feedback about the bootcamp and join different bootcamps which are going on this youtube channel and lot more will be coming in upcoming days so stay tuned and do regular practice be consistent and learn coding thank you so much for joining today's session or watching the recording see you tomorrow with the new topic thank you bye everyone