gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Write FizzBuzz in C.
- User
Write FizzBuzz in C.
- Assistant
Here's a straightforward implementation of FizzBuzz in C:
#include <stdio.h> int main(void) { int i; for (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; }This code defines a loop that iterates from 1 to 100 (inclusive), and for each number checks if it is divisible by 3, 5, or both. If it is divisible by 3, the program prints "Fizz"; if it is divisible by 5, it prints "Buzz"; if it is divisible by both, it prints "FizzBuzz"; otherwise, it prints the number itself. The
printffunction is used to output text to the console. The%dformat specifier is used to print numbers, while\nis a newline character that moves the cursor to the next line.