Programs Needed: Visual Studio
Visual Studio is an integrated development environment from Microsoft. It is used to develop computer programs, as well as websites, web apps, web services and mobile apps. You will need it to complete the example in the workshop.
Console.Readkey(); : tells the program to wait for input before continuing
Variables: stores values, which can be named
var+= - adds a number to the variable
If statements: tells the program to look for a specific case
For loops: use to run the same code a set multiple of times
Print all the numbers from 1 to 100. If the number is divisible by 3, print "Fizz" instead. If the number is divisible by 5, print "Buzz" instead. If the number is divisible by both 3 AND 5, print "FizzBuzz" instead.
Using a for loop, set the int variable to 1 then direct the program to continue until the number is less than 101. Finally, set int to increase by 1 each time.
Your end result will look like this:
for (int i = 1; i < 101; i++) |
Following the code comment, our next step is to check to see if the number is divisible by 3 and to add Console.WriteLine(“Fizz”) in your curly brackets. To do this, compare the variable to 3 by using %, which is the dividing modular operator. To check that the number divides equally with a remainder of 0, add == 0 to the if statement. Additionally, add an else statement to prevent the program from printing the number that is divisible by 3.
Copy the first if statement and paste it directly below it. Change the if statement to an else if statement, replace 3 with 5 and replace “Fizz” with “Buzz”
Your end result will look like this:
for (int i = 1; i < 101; i++) else |
Add another else if statement to your program. Since the program will only run the first statement that applies to your variable, add the new statement above the others. This one will check to see if the number is divisible by both 3 and 5. Add Console.WriteLine(“FizzBuzz”); to your else if statement as the result.
Your end result will look like this:
for (int i = 1; i < 101; i++) else |
Congratulations! You have successfully written your first program!