What is Fizzbuzz?
The Fizzbuzz problem is a well-known programming exercise that is frequently used in coding interviews to evaluate applicants' fundamental programming abilities and attitudes to problem-solving. The challenge is printing a series of integers and replacing the numbers that are divisible by 3 with "Fizz," those that are divisible by 5 with "Buzz," and those that are divisible by both 3 and 5 with "FizzBuzz." Many computer languages, including C, C++, Python, and Java, can be used to tackle this straightforward problem.
Problem Statement Example
Create a computer program that prints numbers between 1 and 100. Print "Fizz" for multiples of 3 and "Buzz" for multiples of 5, respectively, in place of the appropriate number. Whenever a number is a multiple of both 3 and 5, print "FizzBuzz."
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...
98
Fizz
Buzz
Code Example:
C Language
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0 && i % 5 == 0)
printf("FizzBuzz\n");
else if (i % 3 == 0)
printf("Fizz\n");
else if (i % 5 == 0)
printf("Buzz\n");
else
printf("%d\n", i);
}
return 0;
}
C++
#include <iostream>
int main() {
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0 && i % 5 == 0)
std::cout << "FizzBuzz\n";
else if (i % 3 == 0)
std::cout << "Fizz\n";
else if (i % 5 == 0)
std::cout << "Buzz\n";
else
std::cout << i << "\n";
}
return 0;
}
Java
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0 && i % 5 == 0)
System.out.println("FizzBuzz");
else if (i % 3 == 0)
System.out.println("Fizz");
else if (i % 5 == 0)
System.out.println("Buzz");
else
System.out.println(i);
}
}
}
Python
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
You may start by utilizing these code samples to address the Fizzbuzz issue in the aforementioned computer languages.
0 Comments