Given an integer n, return a string array answer (1-indexed) where:

  • answer[i] == "FizzBuzz " if i is divisible by 3 and 5.
  • answer[i] == "Fizz " if i is divisible by 3.
  • answer[i] == "Buzz " if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.

Example 1:

Input: n = 3 Output: ["1","2","Fizz"]

Example 2:

Input: n = 5 Output: ["1","2","Fizz","4","Buzz"]

Example 3:

Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]

Constraints:

  • 1 <= n <= 104

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program fizzbuzz
    implicit none

    integer, parameter :: n = 15
    character(len=10), dimension(n) :: answer

    integer :: i

    do i = 1, n
        if (mod(i, 3) == 0 .and. mod(i, 5) == 0) then
            answer(i) = "FizzBuzz "
        else if (mod(i, 3) == 0) then
            answer(i) = "Fizz "
        else if (mod(i, 5) == 0) then
            answer(i) = "Buzz "
        else
            write (answer(i), '(I10)') i
        end if
    end do

    print *, answer

end program fizzbuzz
Compiled
Executed
Correct
module fizzbuzz

contains

function fizzbuzz(n) result(answer)
    integer, intent(in) :: n
    character(len=10) :: answer(n)

    integer :: i

    do i = 1, n
        if (mod(i, 3) == 0 .and. mod(i, 5) == 0) then
            answer(i) = "FizzBuzz "
        else if (mod(i, 3) == 0) then
            answer(i) = "Fizz "
        else if (mod(i, 5) == 0) then
            answer(i) = "Buzz "
        else
            answer(i) = trim(adjustl(str(i))) // " "
        end if
    end do
end function fizzbuzz

end module fizzbuzz

program main
    use fizzbuzz
    implicit none
    integer :: n

    do n = 1, 10
        write (*, *) fizzbuzz(n)
    end do
end program main
🌐 Data from online sources
def fizzBuzz(n):
    result = []
    for i in range(1, n + 1):
        if i % 15 == 0:
            result.append("FizzBuzz")
        elif i % 3 == 0:
            result.append("Fizz")
        elif i % 5 == 0:
            result.append("Buzz")
        else:
            result.append(str(i))
    return result

The algorithm for this problem simply consists of iterating over the integers from 1 to n, checking the divisibility rules for FizzBuzz, and inserting the appropriate string into the array for each index. For each number i in the range [1,n], we check if it's divisible by 3 and 5 (i % 15 == 0), just 3 (i % 3 == 0), just 5 (i % 5 == 0), or none of the above. Based on the conditions met, we append the corresponding string to the array (i.e., "FizzBuzz", "Fizz", "Buzz", or the number itself as a string).

The code implementations in each language follow their respective syntax, but the core logic is the same across all languages.

🌐 Data from online sources
#include <vector>
#include <string>

std::vector<std::string> fizzBuzz(int n) {
    std::vector<std::string> result(n);
    for(int i = 1; i <= n; ++i) {
        if (i % 15 == 0) result[i - 1] = "FizzBuzz";
        else if (i % 3 == 0) result[i - 1] = "Fizz";
        else if (i % 5 == 0) result[i - 1] = "Buzz";
        else result[i - 1] = std::to_string(i);
    }
    return result;
}

The algorithm for this problem simply consists of iterating over the integers from 1 to n, checking the divisibility rules for FizzBuzz, and inserting the appropriate string into the array for each index. For each number i in the range [1,n], we check if it's divisible by 3 and 5 (i % 15 == 0), just 3 (i % 3 == 0), just 5 (i % 5 == 0), or none of the above. Based on the conditions met, we append the corresponding string to the array (i.e., "FizzBuzz", "Fizz", "Buzz", or the number itself as a string).

The code implementations in each language follow their respective syntax, but the core logic is the same across all languages.