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
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
1 2Fizz 4Buzz Fizz 7 8Fizz Buzz 11Fizz 13 14FizzBuzz
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
temp.f95:5:17: 5 | function fizzbuzz(n) result(answer) | 1 Error: MODULE attribute of ‘fizzbuzz’ conflicts with PROCEDURE attribute at (1) temp.f95:6:28: 6 | integer, intent(in) :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:7:34: 7 | character(len=10) :: answer(n) | 1 Error: Explicit shaped array with nonconstant bounds at (1) temp.f95:9:16: 9 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:15: 11 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:12:53: 12 | if (mod(i, 3) == 0 .and. mod(i, 5) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:13:35: 13 | answer(i) = "FizzBuzz " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:37: 14 | else if (mod(i, 3) == 0) then | 1 Error: Unexpected ELSE IF statement in CONTAINS section at (1) temp.f95:15:31: 15 | answer(i) = "Fizz " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:37: 16 | else if (mod(i, 5) == 0) then | 1 Error: Unexpected ELSE IF statement in CONTAINS section at (1) temp.f95:17:31: 17 | answer(i) = "Buzz " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:12: 18 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:19:52: 19 | answer(i) = trim(adjustl(str(i))) // " " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:11: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:7: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:3: 22 | end function fizzbuzz | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:9: 27 | use fizzbuzz | 1 Fatal Error: Cannot open module file ‘fizzbuzz.mod’ for reading at (1): No such file or directory compilation terminated.
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.
#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.