Tutorialspoint
Problem
Solution
Submissions

Fizz Buzz

Certification: Basic Level Accuracy: 16.67% Submissions: 6 Points: 5

Write a C program that prints the numbers from 1 to n. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz". The function should return an array of strings containing the appropriate responses.

Example 1
  • Input: n = 15
  • Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
  • Explanation: For multiples of 3 (3, 6, 9, 12), we print "Fizz". For multiples of 5 (5, 10), we print "Buzz". For multiples of both 3 and 5 (15), we print "FizzBuzz". For all other numbers, we print the number itself.
Example 2
  • Input: n = 8
  • Output: ["1","2","Fizz","4","Buzz","Fizz","7","8"]
  • Explanation: For multiples of 3 (3, 6), we print "Fizz". For multiples of 5 (5), we print "Buzz". There are no multiples of both 3 and 5 in this range. For all other numbers, we print the number itself.
Constraints
  • 1 ≤ n ≤ 10^4
  • You need to allocate memory for the result array
  • Time Complexity: O(n)
  • Space Complexity: O(n) for the result array
ArraysStringsDeloitteAirbnb
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Create an array of strings to store the result
  • Iterate from 1 to n
  • For each number, check if it's divisible by 3, 5, or both
  • Append the appropriate string to the result array
  • Remember to properly allocate and free memory for each string in the array

Steps to solve by this approach:

Step 1: Allocate memory for an array of n string pointers to store the result.

Step 2: Set the return size to n.
Step 3: Iterate from 1 to n and check the divisibility of each number.
Step 4: If the number is divisible by both 3 and 5, add "FizzBuzz" to the result array.
Step 5: If the number is only divisible by 3, add "Fizz" to the result array.
Step 6: If the number is only divisible by 5, add "Buzz" to the result array.
Step 7: Otherwise, convert the number to a string and add it to the result array.

Submitted Code :