hey everyone welcome back to another video and in this video we are going to look into conditionals and loops in java so when we were learning about flowcharts we saw that you take you know input and output you do some processing and you have some conditions as well we have already seen about input output we've already seen a little bit about like processing like a is equal to a plus one you know c plus plus b plus plus or whatever if you are just starting out do check out the links in the description there's an entire java dsa playlist so this is a video part of that and you can check out all the videos in that playlist the link is in the description make sure like share and subscribe and let's get started so let me create a new project in idea we have done a tutorial on this already you can find the link in the description for the playlist i'm going to create a new project and i'm going to say next from command line like from the template and let me just create a you know lecture number six which is conditions and loops code open com.canal finish pretty cool all right opening it up over here that looks good so let's first look at the conditions part okay the if conditions what are conditions now what is the condition okay let's see why would we require such things so basically let's say you want to you know in a previous example that input a salary and if the salary is uh greater than 10 000 give a bonus of 2000 otherwise give a bonus of 1000 so this check if this is this do this otherwise do this this is something we can do via if statements the syntax looks something like this by the way this is the way you can add multi-line comments so just slash star star slash everything you add in this will be a comment so the the the thing that i'm trying to show you over here is the what is the syntax how do we write this if statement syntax of if statements okay so this is a syntax you write if brackets boolean expression so either true or false it should be like true or false okay and then the body whatever you want to do in it okay so if this condition is true do this and then there's another condition called else do this okay for example i can say something like you know int salary is equal to something like this then i can say if brackets i need a true or false expression of an expression a statement that will give me either true or false so i can say if salary is greater than 10 000 so this will definitely give me true or false salary will either be greater than 10 000 or less than 10 000 this will give me either true or false i can say salary is equal to salary plus 2000 rupees otherwise salary is equal to salary plus 1 000 rupees and then in the end i can just say print the new salary this one is definitely greater than ten thousand so it will be twenty two six four zero zero because salary sorry two two seven four zero zero because it's greater than ten thousand it will do this so in if and l in if and else condition only one of these are going to run only one of these are going to run you can put a debug pointer here try to debug this program and you can see line by line how these things are working so right now we are here salary is equal to this much it's going to check hey is 25400 greater than 10 000 it's going to say yes it is then it's going to go inside yes it is it went inside this if condition means it will not go inside this else condition if and else only one of these will execute you can see this was not executed new salary input and sorry exit out of the program you can also add multiple else if statements in the same thing so let's say you want to add something like um multiple if else statements so let's say you want to add something like if the salary is you know greater than ten thousand then uh salary is equal to salary plus two thousand you can also write it as salary plus equal to two thousand okay this is same as writing salary is equal to salary plus two thousand okay shortcut form otherwise if so you can see else if means i'm combining more than one else sorry if conditions if salary is greater than 20 000 give a bonus of let's say 3000 rupees in the end i will have else condition only in the end i will have else condition only okay cool so this else basically means if none of the above conditions are true then execute this condition else salary plus equal to 1000. okay that is a multiple if else statement so these are conditional like conditions and we can use the if else statements to structure the flow of our program and now let's look into something else which is known as loops so we'll be doing some some nice questions in this session only and that will make it much more clearer we will actually be let's say creating a calculator app program okay in this session only so that is going to be a very nice uh hands-on experience so i'm going to say conditionals filename conditionals another file name and create loops all right so we have scanner i can say input is equal to new scanner we already have seen all these things and then i can say loop okay so what is a loop okay forget about scanner right now let's just do simple loop so i can say i can say something like what is a loop basically so loop is basically let's say the question is print numbers from one to five that's the question you can do it something like this one okay two three four five okay otherwise if someone asks you hey uh print hello world five times so you can do something like this hello world okay five times means copy it five times but imagine someone asked you do a particular task ten thousand times would you write it ten thousand times no right so there's a few tasks that you want to do again and again in like a loop for example print one to ten numbers display all the you know iterate over a particular data type or uh take inputs while the user does not press x which is something we'll do later on so in that case you can do something like you can use loops for that and there are three types of loops we'll be looking into first one being for loop and we'll also be seeing when to use which loop so for loop syntax is so syntax is something like this for brackets just you have your body like whatever you want to do how many times you want to do it initialization condition and increment decrement okay so for example let's say the question is print numbers from one to five okay that is the question let's try to solve this using this in this syntax so four brackets and this will have our body numbers from one to five so i will initialize a number from one to five i can say int number is equal to one while this number is less than equal to five number plus plus or you can say number plus equal to one increment the number y one i will tell you how this thing is working for every time this for loop runs i will just print the number let's try to run this and then i'll explain how this is working one two three four five let's see how this is working then let me add some nice indentation okay basically what is happening over here is that when this line of code is executed int num is equal to one num is equal to one is going to be initialized okay a variable num will be initialized to 1 after that it's going to check is 1 is num less than equal to 5 it's going to be like yes it is then it's going to run this particular command or whatever code that you have in the block after that it will increment it by whatever values you're putting okay so it will increment it by one in this case then it will be now two it will then check again it's two less than equal to five it's gonna be like yes it is okay print num num is two so print two if you then then it will be three then it will be like three is less than equal to five yes it is print three then it will be four so basically this happens only one time after that it will have the check run the body increment have the check run the body increment have the check run the body increment when the num value will be 6 then it's going to check is 6 less than equal to 5 it's going to be like no it's not hence it will not execute what is inside the function that the sorry the for loop body and it will exit out of it you can also do the debugging initially you can say if i go in this it now initialize the value num is equal to 1 and it's going to be like okay print it now it will go again over here like i said you know initialize for the very first time check if it is less than five print it and now it's over here you can see currently num value is one when i go on the next step it will increment it by one two and now the check again is two less than five yes it is less than equal to five yes it is printed similarly three similarly four similarly five now the value will be six so i'm gonna be like is six less than equal to five no it's not it will come out of the loop out of the loop end of the program similarly if i put something like increment in two so first the value will be one then it will be three then it will be five one three and five okay so this is how you uh print the numbers let's say you want to say print numbers from you know one to n where n is something the user will input so i'll just add my scanner over here scanner input is equal to new scanner system dot in i can say n is equal to in dot next end something we have already done before then i'll just run a for loop by the way in intellij id if you write for i it will create an iteration loop for yourself automatically it created so i can say num starts from zero should be less than equal to n num plus plus and just start from 1 also because i want to input print numbers from 1 to n right let's say print number i'll just add a space over here so that i don't add it in a new line every time and that is it let's try to run this let me hide this code so it will ask for me to run to input a number let's say 100 so all the numbers from 1 200 it will print okay very basic stuff let's say i want to let's say i wanted to print hello world and number of times so i can just say if you want to print hello world and number of times lowered let's say i'll tell it to print hello world 10 times 10 times its printing hello world okay that is the for loop the next loop we'll be looking into is called a while loop while loops syntax is something like this syntax is something like this so you have the while loop condition over here you write while then you write the condition and then body that's it that is it let's try to convert this program of one to five numbers into a while loop so i can say int num is equal to one so basically i'm converting this program into a while loop so that is why you will be able to see the reference like how this is getting converted to a while loop basically the for loop we wrote before we are converting it into a while loop so i can say while so basically the initialization part i'll just copy paste this for reference so for the initialization part that will come outside the while loop this condition will now come inside the while loop this body will come inside the body of the while loop and this increment thing will also come inside the body of the while loop that's it let me try to hide this thing let's try to run this oh it's actually asking for an input let me also hide that one two five one two three four five how this is working let's see debugging so here okay so here it's saying num value is one and initialize that okay so num is initialized to one is one less than equal to five it's saying it's true hence it will go inside this while loop it will print one and then it will increment the value of one by like plus one to it it will now become two that's true num becomes 2 it's 2 less than equal to 5 yes it is print 2 and make it 3 3 less than equal to 5 print 3 make it 4 make it 5 make it 6 is 6 less than equal to 5 no hence it will not go inside the while loop it will exit out of the while loop program over okay so that's the while loop now the question you may ask yourself is how do we know when we uni when we need to use the while loop when we need to use the for loop you can use both of these anytime you want okay it's totally up to your preference one of the things that i would recommend like something that why these two exist is because you need to run a while loop when you don't know how many times the loop is going to run and run and use a for loop when you know how many times the loop is going to run if the question is something like print the first 10 numbers then you will use a for loop if the question is something like keep taking input from a user till the user does not press x here do you know how many times the loop will run no depends on the user till the user will not press an x that many times of amount like the loop will running so in that case use a while loop one more loop is there known as a do while loop okay let's see so the next one is a do while loop syntax is something like this do this thing while condition okay let's try it out i also explain the difference between okay so let's say we take int n is equal to one i can say do print n and n plus plus while n is less than equal to pi i'll explain what the difference is don't worry i think it's pretty obvious what it's doing one two three four five why would we want to use a do while loop and when would we want to use a while loop that is another good question let me just write the body syntax over here okay so do while and while the do-while loop and the while loop have one difference in it in the do while loop the program is going to execute the loop is going to execute at least once at least once it will execute so it will execute at least once so for example if i write something like uh you know let's say i remove this thing all these things and i just write print hello world and here i write while n is not equal to 1 so here even though n is equal to 1 this should not execute but it will because first it will execute the body then it will make the change checks so basically the idea is it will execute at least once and after that it will only execute when this while condition is true after that so once it will always execute let's try it out prints hello world so basically this is what happened n is equal to 1 it will do this thing first it will print hello world after that it will check that again if you want to run this loop this condition should follow while n is not equal to 1 it will say n is equal to 1 hence loop will end okay so in cases in which you definitely have to have one time the loop running in that case you can use a do while loop for example if you want to say take input of so many numbers till the user input some other value like x or something okay in that case once the value will all obviously be input right the value will always be there like for the very first time that you can use a do while loop now let's try to solve some questions over here because questions are important in order to understand how these things are working internally so let's say we create a question like largest so you will be giving three numbers and let's say public static void main scanner input is equal to new scanner system dot in so basically you are going to input three numbers okay so i can say a b and c and a is equal to in dot next end and b is equal to in dot next end and c is equal to in dot next three numbers are here question is find the largest of the three numbers how do we do that so let's look at how we are going to solve this problem you can also create a flowchart for it if you like but uh imagine that you have three numbers let's say you know a is equal to 10 b is equal to 20 and c is equal to 30. you have to figure out which is the largest number from all these three numbers so one way we can do this we can let's say assume that this is the largest number okay we can take a maximum value we can say maximum is equal to let's say 10. then we are going to check this with b is b greater than maximum that the current maximum that i have is b greater than that it's going to be like uh you know yes it is so in that case it's just going to say that my maximum now is equal to b then it's going to make another check if condition if c is actually greater than my maximum is 30 greater than my maximum which is currently now 20. it's going to be like yes it is then max is equal to 30 or c means 30. that is my answer if you aren't able to see here you can see there we go cool so let's try to code this example so here in this case i will say let's say int maximum is equal to a then i can say if b is greater than maximum i can say maximum is equal to b otherwise i can say if let's say c is greater than maximum max is equal to c and then i have output say maximum let's give it three numbers 10 20 30. 30 is the maximum one let's give it something else 56 12 79 79 is the maximum one 33 789 65 789 is the maximum one that is what is happening it's saying initially let's say it will say let's let's try to you know run this code only debug it and see how this thing is working input cool a is equal to 33 so it's saying max is equal to 33 okay currently max is equal to 33 now it's checking hey is b greater than maximum value it's going to be like yes it is then the new maximum value is going to be b now the new maximum value is going to be 789 that is true you can see maximum now 789 it's not 33 anymore now it's checking again is c greater than the maximum value is 65 greater than the maximum value which is 789 no it's not hence this will not execute this will this if condition will be false this will be false so this will not execute hence the output will be 789 program ends very simple stuff and one more thing like before we move on to the next question um there are many ways to solve one pro one one problem okay there can be many ways if you don't want to take the like the you know the maximum one as uh as a or whatever you can say initially something like let's say let me just let me just copy this thing okay let me just comment this out no copy i can say let's say another method int max is equal to 0 and here you can say that if a is greater than b max is equal to a otherwise max is equal to b so now in this maximum value it will actually store either a or b and in the next line you can say if c is greater than max max is equal to c same thing here it's actually trying to suggest us intellij is saying you can optimize this loop ah yeah you can use a maximum the math.max call that is also something you can do so basically math you know the math class has something like this so if i comment this out you can say my int max is equal to math.max a and b so it's basically going to return that to the maximum value from these two okay so this is going to contain the maximum value of these two and i can again pass this in math.max or c okay so this is basically saying uh basically if i do math.max let's say 34 and 757. so it's going to print 57 let me comment this out it will give an error because uh did not comment out this thing now it should run 57 that is what math.max is doing okay if i replace this with my variable max take the inputs now let's try to run this if i give three numbers 10 30 27 it will print 30 why because it is it is going to say math.max of 27 and whatever the maximum value is of 10 and 30. so it's going to be like math.max of 27 comma 30 means 30. that's my answer now let's go into the next question let's do something let's say we input a character a letter and basically any an alphabet and it will tell us whether the alphabet is uppercase or lowercase let's try to make that program that'd be pretty pretty nice so you can say type check or something like case check or whatever okay case check click start void main and here i can say scanner input is equal to new scanner system dot in let's take a character input there is no such thing as in dot next care so for that we are going to have to take a string input and take the first letter of it okay so that's the way to take characters sorry uh ch is equal to in dot next dot trim dot car at zero what am i doing over here trim basically means that i'll just hide this for now let's say i say print in dot next this is just going to print the next word so if i just run this it's just going to print the next word okay it will take the next word over here and it will it will print it in this function only so i can say hello so it will print hello nothing new over here what is dot trim dot trim basically means remove all the extra spaces that are at the end of these words so i can say if i take uh you know next for let's say if i do trim over here now let's say if i print it let me do a let me do a space hello so you can see extra space was removed okay because that is not what we want we basically want that okay we are going to be inputting a number like a character basically not a number so i can say i'm going to be inputting k so this k is actually a string input you cannot take like a car input it will convert it into car when you use this method called care at 0. don't worry about this right now because string is a separate module of this course so we'll look into it later on like what is a carat and everything but in case in short if you want to understand it's not that complicated this is a string care at basically means give me the character at this index of the string string you can imagine it as a character array array is actually the next topic so it will be much more clearer than but in arrays index starts from zero so if i'm saying something like uh hello okay so if i'm saying let's say string word is equal to something like hello and then i print word dot car at zero this is going to print print which is the character at this words zeroth index h it will print h h which is the character at index number 2 so 0 1 2 l it will print l okay so this car act is actually returning a character type you can see over here character type okay so if it is returning a character type then we already have learnt about data you know data types so i can store it in a character type that's why it's working now i will input a character and it will be stored in the ch input let's say l l type character question is you need to check whether this is lowercase or uppercase so the ch and everything you can use like the sky values and stuff but we can also do something like if since it's a character i can directly compare it with uh the values of you know lowercase a and lowercase z if it lies in the range of that because in the type section we saw that it actually converts it into like you know checks when integers so we already saw that if you haven't check out the link in the description below we have an entire entire session on that on types and everything and how these compare this is greater than equal to a and ch is less than equal to z basically this is something we did not cover before and so basically if you want to add two conditions like both of these conditions need to be true you can use the and and and operator okay so it will only be true if this is true and this is true okay if if if we go into our conditionals again and if i just hide this thing so you can see multiple i can say if let's say let's say int a is equal to 10 and in b is equal to 20. so i can say if a is equal to equal to 10 and b is equal to equal to 20 so is a equal to 10 yes it is b equal to 20 yes it is it will print if you say or you can also add or or basically means either one of these should be true so if i make this one false so even though a is equal to 10 b is not equal to 20 it will still run because this is or means any one of these to be true we will do this in detail when we do bitwise operators okay these are bitwise operators we'll do it in detail hello world okay we'll do it in detail there's a separate uh section for this and if i do something like um okay so you you already learned about and and or this double equal to basically compares the value of this and there's one more called not equal to if a not equal to 35 print this is a equal to 35 no it's not so hence this is true a should not be equal to 35 that's true it is not equal to 35 it's 10 hence it will print hello world so you can multiply you can add so many if so many so many conditions you can add in just one single if condition okay like we are adding 2 over here if a is greater than a the value like if it lies between the range of capital small a and small z i can say print lowercase otherwise i can say print uppercase let's try to run this program you can add any number oh sorry any letter h lowercase sledge if i add something like capital h uppercase simple stuff all right cool let's move on to the next question now let's look into another problem which is the fibonacci problem so what are fibonacci numbers fibonacci numbers are basically let's say they start by zero and one starts from zero and one let's say let's say the zero fibonacci number first fibonacci number and the series then continues by adding the previous two numbers so basically the next number is going to be what sum of the previous two numbers zero plus one two zero plus one is one one comes over here the next number is going to be the sum of the previous two numbers one plus one is two two will come over here next will be sum of previous two numbers two plus one three three will come over here next we'll do some of the previous two numbers three plus two five five will come over here next will be the sum of previous two numbers five plus 3 8 then 8 plus 5 13 and so on that is what we want to do okay so we have to write a code for this so basically what is happening is that we will be having the first number as this one like the 0th the first one number we can say a is equal to 0 b is equal to 1 and we will be having a number n is equal to let's say let's say 7. so the the question is find the nth fibonacci number so basically the zeroth first second third fourth fifth sixth number seventh number so the answer for this should be equal to thirteen if i say give me the sixth fibonacci number answer should be eight that is what we're going to do and here we know how many times our loop is going to run we know we need to find the nth number we will have the value of n because we will input it hence we will use a for loop for this so we will initialize these two variables then we will use a for loop till you know till it runs till like seven or whatever and uh then basically we will just uh keep on adding and updating these two values for example the next number will be a plus b which is equal to one comma a will then be replaced with the b and b will be replaced with this so a n will be 1 b will be 1 next will be 1 plus 1 2 a will be replaced with the the next one this one and b will be let me make it much more clearer by making a table let's make it much more clearer by making a table so a comma b initially it's 0 it's 1 and this is my series that is currently ongoing okay so now when i do 0 plus 1 it will be 1 and b will come over here and the new b is going to be this thing that i have just added 1 plus 1 then will become 2 b will come over here this will be the one that i have currently added 2 2 plus 1 3 2 will come over here and 3 will come over here 2 plus 3 5 see very simple 3 will come over here and 5 will come over here 5 plus 3 8 it will keep on going on going on going on something like this let me just let me just show you the you know in case you are unable to see the now i think it might be visible like this this one now previously i mentioned like when you know how many times a loop is running you can use a for loop but one more case in which you might want to use a while loop is when the code is looking cleaner sometimes the code will look much more cleaner when you're using let's say a while loop over or a for loop so here in this case i'll just add a i'll just add a scanner class is equal to new scanner system dot in and here i'll just input my nth fibonacci number next int okay so now the initial ones that we'll be having the one that you know the the two numbers one and zero i can add is that let's say into uh p is equal to zero means previous and int i means the current index equal to one we already have the first two numbers so our count can start from let's say two count is equal to two okay and then i can say while count is less than equal to n you want the nth fibonacci number okay so while count is less than equal to n what do we need to do we need to add uh we need to basically the answer in the end is going to lie in the it's like the nth fibonacci number right so that will be i so here uh how do we make sure that that happens we need to add the previous two numbers that is what we want to do so basically i can take a temporary variable temporary variable of i because this temporary variable now previous is actually going to be equal to you know when we were doing in the previous example we are doing this will come over here b will come to a means the new a is going to be equal to b that is what we are going to do over here okay so if i have something like this i can say that you know p will be equal to i previously but why am i taking this i in a temporary variable because we want to make sure that we want to add these two numbers right we want to say i plus p i can store this in i only means the next number is i is equal to i plus p that is it count plus plus that's it that's my fibonacci number if you don't want to like do this is i n uh stuff so i can say this is a i can just write it in the you know format of what we which we wrote in the program as well so i can say this is let's say my a just renaming it to b okay so here i'm saying basically if you want to say next is equal to b plus a okay so the third fibonacci number it will be 1 plus 0 and then i am just updating the values the a is going to become b that is true because temp actually stores the value of b why have i taken 10 because if i do a is equal to b over here this will not be the value of b that it had previously this will be the one that is now updated that is why i am taking a previous value over here okay try to run this on the paper it will be which is exactly the same thing as we were doing in the previous example and i literally ran it like line by line and that is exactly what it's doing so this is the next number and then i'm i'm just updating a and b a is being updated to b that is correct a is being updated to b b is being updated to whatever next number i have added that is true b is being updated to whatever next number we have added in the end i'll just print b we were saying seven fibonacci number it should give us in that example we had what did we have for 7th fibonacci number 13 we are getting 13. six fibonacci number should give me eight we're getting eight cool that is that is uh pretty much about it and uh we will learn more about like the time complexity and everything when we actually do time complexity we'll also solve this in many other ways i'll also teach you how to find the fibonacci nh fibonacci number without using any loops in just one single step i'll be able to you will be able to find the ns fibonacci number that is going to come in some bit more advanced topic which is in during the spacing time complexity analysis so stay tuned for that let's look at the next question now let's say for the next question we say something like um there's a number given to you let's say number n is equal to 1 3 8 5 7 5 um seven eight seven nine this is a number given to you okay and you basically need to find how many times the number seven is occurring in this number so the answer should be 3 how do we do this problem pause this video and then figure it out and now we look into the solution ok so basically the idea to do this thing is whenever you get such questions where you actually have to deal with individual digits you are going to have to check individual with digits for you know obvious reasons because you are checking individual digits now how can we get individual digits from this there can be many ways you can convert this entire data type to a string data type and then you can iterate upon it or you can do something much more simpler if you take the remainder of any number by 10 you are actually going to get the last value you are getting the last value let's check it out if i say let's say n is equal to 1389 and if i divide it by 10 so i can say 1 3 8 9 divided by 10 so i can say 10 into 138 gives me 1 3 8 0 remainder is 9 isn't this same as the last digit hence n modulo 10 is going to give you the last digit remainder of that number with 10 will give you the last digit so here we can do something like if i try to you know take a smaller example in order to make things clear so i can say let's say n is equal to 1 3 8 3 9 count the number of threes okay so in this case the answer should be 2 let's try to see this how this works so basically i'm going to say while you know n is greater than 0 i can say know my remainder is going to be equal to n divided by 10 then i'm going to check this is actually the last digit okay so i'll check is my remainder equal to 3 if it is then i'll say count plus plus let's say initially count is equal to 0 okay and in any case i'm just then going to reduce this number as well so for example i have something like n is equal to 1 3 8 3 9 remainder will be let's say 9 now i need to actually remove this 9 from this number as well right because then i will check for this thing 1 3 8 3. how do i do this i can just say n is equal to n divide by 10 that is true because if 1 3 8 9 1 three eight three nine i divided by ten it will give me one three eight three okay then for one three eight three i will check modulo 10 is uh so basically this is what i'm doing basically it will say one three eight three nine it will say modulo 10 going to give me nine and let's take a count over here so count initially zero is nine equal to three no it's not then i'm just going to not increment the count variable and i'm just going to divide it by 10 so now if i divided by 10 this will be 1 3 8 3 now remainder of 10 will give me 3 3 equal to 3 yes count will be 1 this will be now reduced to 1 3 8. remainder is 8 nope it will now reduce to 13 last digit remainder 3 yep count increase to 2 this will be 1 last area 1 equal to 3 no it's not it will then be again reduced to 0 because 1 divided by 10 will give me 0 in the integer okay since we're storing it in integer it will be like 0.1 points will be cancelled out only 0 will be there while n is greater than 0 is the condition hence this will terminate and our answer will be available to us i can say n is equal to n divided by 10. that is it let's try to code this solution so if you're trying to code this solution we can also do some really optimized things like you know bit masking and shift operators and everything we'll do that we'll do same questions later on count nums for exactly what main i'm not taking a let's say i'm not taking an input i can just say 4 5 three five three six or whatever then i can say while n is greater than zero similarly similarly the same thing we did in the previous you know and the the drawing part i can say my count is equal to 0 initially in the end my answer is going to be equal to what count and here i am just going to say int remainder is equal to n modulo 10 if remainder is equal to equal to 5 count plus plus n is equal to n modulo oh and divide by 10 you can also write something like this to make it shorter has two fives so answer should be two that is correct let's add one more five over here answer should be three let's add no fives over here answer should be zero zero okay so very simple solution and now let's look into the next problem now let's try to do another important question let's say you are given a number that is let's say question that number is given to you that says the number is 2 3 5 9 7 the answer should be the reverse of this number so the answer should be 79532 pause this video think about it and it's going to use the same remainder approach and then watch the rest of the video to get the answer okay i hope you tried it out and let's see how we're going to do this so basically the idea is same the same while loop that we used before we're going to use the same thing so basically i'm going to say that my result initially it's equal to 0 before i write the code let me show you how it's going to work so basically the idea is i'm going to take the remainder every time while the number is greater than 0 i'm going to take the remainder i'm going to add it in my answer so initially let's say the answer is 0 okay and my number like the entire thing is over here 2 3 5 7 9 when i take the remainder for the very first time my remainder will be 7 i'll add it to my answer answer will become 7 okay then i'll take the remainder again it will now be 9 before adding this is the same thing we were doing previously only one extra step is needed when you are adding this number in your answer make sure you do you have to basically add the number in your answer so that it it should be like 79 over here right so basically you need to shift this seven towards the right so towards the left so what you need to do is you have let's say seven over here okay and you have nine over here that needs to come over here now how do we make sure 9 comes over here we can just basically can we not do if i do something like 7 into 10 plus 9 what is this going to give me 70 plus 979 that's why i take another number 5 now if i do again now it's 79 so 79 into 10 plus 5 which is 795 make sense then 3 so 795 into 10 plus 3 which is 7953 then again last remainder 2 7 9 5 3 into 10 plus 2 which is 79532 last remainder then now be 0 isn't this my answer okay very simple solution same remainder same dividing only thing we are just going to multiply the answer by 10 and add the remainder in order to get the answer at that particular point when the loop finishes this will be my entire answer let's try to code this program okay so let me create another file i can say reverse static void mean in number is equal to something like this random number then i'm going to say what in answer is equal to initially 0 in the end i'm just going to print the answer and same thing that we did previously while number is greater than 0 in remainder is going to be n modulo 10 and divide by 10 equal not n sorry num num modulo 10 num is dividing by numbers then getting divided by 10 for the same reasons in the previous example and i'm just going to add it in my answer so answer is equal to answer into 10 plus remainder that is it so simple so simple eight 97482 should be answer nine seven four eight two how simple is this one two three four five six should give me six six five four three two one six lakh fifty 54 312 321 very simple stuff okay cool let's look at the last question which is going to combine all these things together which is known as building a calculator app not an app but a program calculator program so let's try to make a calculator program so i can say calculator and then public static void main scanner in is equal to new scanner system dot in if you're just watching this video make sure you check out the playlist all these things have been covered over there what is scanner and everything okay so the idea is that we're going to take input from user till user does not press x or x okay till that time we are going to keep taking like input from the user first we are going to take the operation from the user and like plus minus into or divide and uh whenever the user inputs x the program will stop so basically i can create an infinite while loop over here that while true okay while true care ch take the take the character input take the operator as input so i'm just going to say ch is equal to what uh or my operation my operation uh operator in is equal to what in dot next no no no no next dot trim dot carat zero now basically i'm just going to make a check i'm basically going to check that if my operator is equal to equal to you know plus or my operator is equal to equal to minus or my operator is equal to equal to multiply or my operator is equal to equal to divide because if you add something as an op if you add this as a character oh this can also be a you know remainder thing so i can say or if my operator is equal to equal to modulo you can also you know get right but if you add something like and that should not give you any answer right you're not using bitwise operators and if you had something like dollar okay so what what is that dollar dollar is nothing so that is why we are taking only these five basic operators okay now what we're going to do is we're going to input two numbers okay especially say int num1 is equal to in dot next int in number 2 is equal to in dot next end taking two numbers now now i basically need to make a check so basically i can say that if operator is equal to equal to addition then in that case i can just say print or not print i can just say i take my result over here somewhere i can say my end answer is equal to zero and in the end i can just say print answer ah makes sense because uh i haven't added like uh the condition to break this while loop right now we'll do that because there's an infinite while loop and we have to make a condition to break it which is uh whenever the user inputs x because currently that there's no condition for that and it will keep on running infinitely hence we need to put a condition for that as well anyway in this case i can say answer is equal to um what num1 plus num2 that is it then i can say just copy paste this if operator is equal to equal to minus answer is equal to num1 minus num2 if operator is equal to equal to you know multiplied i can say answer is equal to num1 into num2 if operator is equal to equal to divide i need to basically here make a check okay i need to make a check that uh you know if number 2 is not equal to 0 because you cannot divide by 0 then i can say something like answer is equal to num1 divided by num2 okay and only thing remaining now is something like modulo see i can just add answer is equal to num1 modulo num2 okay now the last thing is that uh if this is not the case okay these are not the operators then basically it means that you input something like x so in that case you can say something like um you know here i can add so it's going to take a character input if it is not in this it basically means it's either an x or something else so i can say else if operator is equal to x or operator is equal to capital x break the while loop otherwise it's let's say not a particular operator or not a you know either x or capital x in that case you can just print invalid operation something like that let's try it out let me add some let's add some like uh commands or like the messages over here as well you can say enter the operator new line let's say enter two numbers this out enter to numbers okay new line over here let's try to run this in an operator let's say plus enter two numbers three and four it should actually display the answer as well at every point okay so basically i can display the answer i forgot to display and this will not uh yeah this is yeah yeah can display the answer over here after every operation basically it was outside the while loop so it will only display like one answer but i need to display at every point of time i don't think i need the extra lines over here so i can just remove the new line statements okay let's try it out enter the operator plus enter two numbers three and four answer is seven it is asking for the operator again because you can see uh there's an infinite while loop so infinite while loop it says you know enter the operator it's saying okay input it if it lies over here cool and then it's going to ask for you know it's going to print the answer and then ask for the operator again if it lies over here cool because i initially mentioned there are three conditions one if condition else if condition and else condition only one of these is going to run so this normal one is running this break will never happen this will only happen when you input x then it will print the answer take the input again print the answer take the input again when you press x let's say i take star and i add 6 and 2 should give me 12 12. now let's say input x program over so when i input x it's going to say does x lie over here does this condition satisfy x it's going to be like no it does not then it's gonna check if else else if operation is equal to it's gonna be like yes it is break outside the while loop okay i can also try some other operator like this saying invalid operation enter the operator again okay that's basically the idea very simple stuff you can try to make a flowchart of this if you want it's going to be a pretty big but it's going to be good practice and just a lot of copy pasting over here and yeah simple simple calculator program make sure you check out the links in the description i'll be uploading the assignment and notes and everything and uh in the next session we'll be doing arrays if you have if you are new then all the videos are available in the playlist below in order so if you're looking for videos in order check out the links in the description make sure like share and subscribe share with your friends we're going to be doing some amazing work like in this session i did so many questions we'll be doing so many questions for every other topic from the next session we'll be doing arrays which is going to be bringing us very many amazing things sorting searching and i know com interview problems as well so make sure you subscribe and i'll see you in the next video