Fizzbuzz Implementation in C
FizzBuzz is a well-known programming problem that is widely used to determine fundamental programming skills. With a few exceptions, the programmer has to develop a code that produces integers from 1 to a certain limit (typically 100): For numbers divisible by three, the program should output "Fizz" rather than the number and "Buzz" for numbers divisible by five. The program should return "FizzBuzz" for integer values divisible by 3 and 5.
The Fizzbuzz issue is a programming task that is commonly used as a means of testing for software engineers. It is a basic game in which you must count from 1 to a certain number while following to the limitations for each number.
The Fizzbuzz game follows these guidelines:
- Say "Fizz" if the number is divisible by 3.
- Say "Buzz" if the number is divisible by 5.
- "FizzBuzz" should be used if the number is divisible by both 3 and 5.
If not, just say the number.
For example, the Fizzbuzz game should produce the following results when counting from 1 to 15: Fizz 1, Fizz 2, Fizz 4, Buzz, Buzz, 7, 8, 11, 12, 13, 14, FizzBuzz. The Fizzbuzz issue is widely used in interviews to measure a developer's ability to build clean and efficient code. It is a good task for beginners to practice their programming skills and learn about control stream explanations and dependent declarations.
Here is an example of how to implement FizzBuzz in C:
Code
#include <stdio.h> int main() { int limit = 100; for (int i = 1; i<= limit; 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; }
In this example, a for loop is used to run through the integers 1 to 100. The if-else lines within the loop determine if each number is divisible by 3 and 5 or both, and then use the printf() function to print the appropriate text or number to the console.
The output of the above program:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Except for integers that are divisible by 3, 5, or both, the numbers from 1 to 100 are printed out in this format. Instead of the number, the relevant string ("Fizz", "Buzz", or "FizzBuzz") is presented for such numbers.
Time complexity: Its time complexity is O(n), where n is the maximum number range to be printed.
It is because the program uses a for loop to perform a fixed number of operations (basic arithmetic and conditional checks) for each integer in the range from 1 to n since the program's number of activities corresponds to the info size (for example, n) its time complexity is linear in terms of the input size or O(n).