Coding Challenge Fizz Buzz with .NET Core
Video
Links Mentioned
Transcript (Unedited)
what's going on good people so this video will be another coding challenge this coding challenge will be doing the fizz buzz you've probably heard of this one before uh believe it or not i've actually done this in an interview before i think it had different words with it but it was still the same concept without further ado let's go ahead and jump it right into it [Music] all right so here is the coding challenge for this video and it is to write a program that prints the numbers from 1 to 100 or for multiples of 3 print fizz instead of the number and for multiples of 5 print buzz for multiples which are for numbers which are multiples of both three and five print fizz buzz that being said uh is pretty straightforward as you see there's an example here so let's go ahead and get right into it as like with the last coding challenge i will be doing this in net core and all right so now that my system is decided to start responding once again um what i'm going to do is i'm going to open a new actually i need to create a new project i've created a repository with all of these coding challenges in it so a link to that repository will be down in the description below if you've missed a previous coding challenge all that code will be in the same repository i figured i would put all of these coding challenges into the same repository that way um they have a central location for where they can be easily found all right so i'm going to create a new project so again using the.net core we're going to do net new console and we're going to give it a name of fizzbuzz all right and that should create a new folder called fizzbuzz as well as set up everything we need for the project so if we cd into that directory we have our project now let's go ahead and create a uh get ignore file for this particular directory that way we don't commit our project files such as the actual executables to the repository now that we have done that we can go into visual studio code into the appropriate directory and open the project that we just created from the command line and then we can open our program.cs i'm going to update the namespace to customize it to my particular setup or how i prefer to do things and i'm also going to remove the hello world out of here and it wants to reboot through the assets required so we'll go ahead and click yes on that and now so we have a program that prevents prints the numbers from 1 to 100 so what i will do is to create a for loop we'll do it i starting with 1 and while i is less than or equal to 100 we will increment i um this is your basic for loop and this for loop replies to regardless of what language that you're doing um the next step so we need to determine whether the number that we're currently on is a multiple of three a multiple of five or a multiple of three and five so the way i'm going to do this is to do a uh we'll call it a string with output and we'll set it to an empty string and then if i is divisible by three then we will add to that string fizz all right and uh also do i know you're thinking well should you do it else if no so what i'm going to do is i is divisible by 5 then whoa all right we'll do an output of we'll do plus equals here buzz and looks like i'm missing some quotes all right and let's see if i okay i think i may have missed done this so if we do i divided by five and that equals zero that should work maybe i have the wrong type of slash there we go all right so i had the wrong slash on there all right and so what we're doing is we're basically doing division so if i divided by 3 equals 0 which i don't think this is quite right i want to use the modulus operator but it seems to be complaining about the modulus operator so we'll go modu modu lust operator c sharp all right and so we'll go to the internet and kind of look into this a little bit further to make sure we're actually using the right thing um because what i want is so let's see ah okay so we want is we want to do the modulus so we want the modulus is basically the uh mathematical representation of the remainder so when you're discussing um so let's say if you have 15 15 divided by three would be five with a remainder of zero and so that's where the modulus comes in and so uh i i think originally when i was typing this i did have the i percent three but i didn't have the equation of what to check and so it kind of flagged that let's see if we go back here um yeah so i didn't like that because it couldn't convert that type so this is actual condition so it can convert that and so the reason i'm doing it this way is because i want the fizz buzz to be able to show up so you have to do it in a certain way like i mentioned i've done this in as an interview question once before um so it took me a little bit moment but once i worked with the interviewer and discussed some things then we did get it correct and uh outputted all right so now that we have our fizz buzz portions what i want to do next is check to see if the output length is greater than zero and if it is then well excuse me correction let's do this so we want to do if the output link is equal to zero which means that none of these conditions were returned true then we want to set the output equal to the i dot tostring value and then we want to console.writeline uh the value of output all right and so don't want to remove too many braces so if we do format and trim if i run this in the terminal and it may ask me to do some other stuff before it actually runs off maybe not all right so it is building it is now running there we go all right so we have one two and if we come and pull up our image to kind of do a comparison so we have one two fizz four buzz is seven eight fist buzz 11 is 13 14 fizz buzz and that's where our example stops at but the pattern continues forward where it does the fizz buzz so let's come back to this code and do a little bit more elaborate walkthrough so we're basically we're do looping over the numbers one through 100 and we're initializing every time we start with another number we're going to set the output value to a empty string which is this here just two votes with nothing in the middle and then we're going to check to see if the number is divisible by 3 and if so does it have a remainder if it does have a remainder it will not output it will not add fizz to the output if it is if it does not have a remainder it will set that to the remainder will be zero then it will add fizz to the output then here we're going to do the same thing as we did for three but we're checking for five instead and this time since we already we may have fizz in the previous check we want to if this condition is true we want to add buzz to the output so we're going to do the plus equals operator now this operator is the same as if you were to do output equals output plus buzz all right as soon as i can get it type so that's the this condition is the same as this condition this is just a more concise way of typing this same statement all right and then the last check will be if the output length so the length of this variable is equal to zero which the length by default would be zero then we know that it does not have a fizz or a buzz contained in it and so based off of that we can set the number equal to i mean excuse me we said the output equal to the number that was currently being done in that for loop and then the last part is to print out that number to the console all right and so uh that being said i'll run it once more again just so you're able to see it um if you have any questions regarding this particular project drop those down in the comments below a link to the repository where you can find the source code for this will be available also and until next time peace out oh and well as you see it ran successfully again the 99 100 there you go all right peace out