Introduction:
The FizzBuzz problem is a common programming exercise where you are asked to print the numbers from 1 to 100, with the following exceptions:
- For multiples of 3, print “Fizz” instead of the number.
- For multiples of 5, print “Buzz” instead of the number.
- For numbers that are multiples of both 3 and 5, print “FizzBuzz” instead of the number.
This program demonstrates how to implement the FizzBuzz logic using the Go programming language.
Objective:
The objective of this program is to print numbers from 1 to 100, applying the FizzBuzz rules. You will gain experience with conditionals and loops in Go, and learn how to structure a simple program with clear output.
Go Code:
package main
import "fmt"
// Main function to execute FizzBuzz
func main() {
// Loop through numbers from 1 to 100
for i := 1; i <= 100; i++ {
// Check if the number is divisible by both 3 and 5
if i%3 == 0 && i%5 == 0 {
fmt.Println("FizzBuzz")
} else if i%3 == 0 { // Check if divisible by 3
fmt.Println("Fizz")
} else if i%5 == 0 { // Check if divisible by 5
fmt.Println("Buzz")
} else { // For numbers that are neither divisible by 3 nor 5
fmt.Println(i)
}
}
}
Program Explanation:
The program consists of the following key parts:
- Importing the fmt package: The “fmt” package is imported to enable formatted I/O operations, like printing to the console.
- The main function: This is the entry point of the program. The “main” function contains the loop and the logic that performs the FizzBuzz operations.
- The for loop: The loop iterates through numbers 1 to 100 using the syntax
for i := 1; i <= 100; i++
, where “i” is the loop variable. - Conditionals: Inside the loop, there are
if
,else if
, andelse
statements to check the divisibility of “i” by 3 and/or 5:- If a number is divisible by both 3 and 5 (i.e.,
i%3 == 0 && i%5 == 0
), it prints “FizzBuzz”. - If a number is only divisible by 3 (i.e.,
i%3 == 0
), it prints “Fizz”. - If a number is only divisible by 5 (i.e.,
i%5 == 0
), it prints “Buzz”. - If none of the above conditions are true (i.e., the number is not divisible by 3 or 5), it prints the number itself.
- If a number is divisible by both 3 and 5 (i.e.,
How to Run the Program:
Follow these steps to run the FizzBuzz program written in Go:
- Install Go: Make sure you have Go installed on your system. You can download and install it from the official Go website: Go Downloads.
- Create a Go file: Open your text editor and create a new file named
fizzbuzz.go
. - Paste the code: Copy the Go code from the section above and paste it into your
fizzbuzz.go
file. - Open the terminal or command prompt: Navigate to the directory where your
fizzbuzz.go
file is saved. - Run the program: In the terminal, run the following command:
go run fizzbuzz.go
- View the output: The program will output the numbers from 1 to 100 with the appropriate substitutions (“Fizz”, “Buzz”, or “FizzBuzz”).
Example Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
... (and so on up to 100)
Conclusion:
The FizzBuzz problem is an excellent way to practice basic programming concepts like loops and conditionals. In this case, we’ve implemented it using the Go programming language. The solution efficiently checks for multiples of 3 and 5 and displays the appropriate output. It’s a great exercise for beginners to strengthen their understanding of logic and flow control in programming.