Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Write FizzBuzz in C.

  1. User

    Write FizzBuzz in C.

  2. 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 printf function is used to output text to the console. The %d format specifier is used to print numbers, while \n is a newline character that moves the cursor to the next line.