From e37aa60781fce932af4c149370b0c4ab2759fa68 Mon Sep 17 00:00:00 2001 From: Disha Thakurata <146114938+dishathakurata@users.noreply.github.com> Date: Fri, 6 Dec 2024 00:07:06 +0530 Subject: [PATCH] Create Fizz Buzz --- Fizz Buzz | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Fizz Buzz diff --git a/Fizz Buzz b/Fizz Buzz new file mode 100644 index 0000000..1a40205 --- /dev/null +++ b/Fizz Buzz @@ -0,0 +1,68 @@ +//Fizz Buzz + +import java.io.*; +import java.util.*; +import java.util.ArrayList; + +class StringArray { + public static String[] input(BufferedReader br, int n) throws IOException { + String[] s = br.readLine().trim().split(" "); + + return s; + } + + public static void print(String[] a) { + for(String e : a) { + System.out.print(e + " "); + } + System.out.println(); + } + + public static void print(ArrayList a) { + for(String e : a) { + System.out.print(e + " "); + } + System.out.println(); + } +} + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine()); + + while(t-- > 0) { + int n; + n = Integer.parseInt(br.readLine()); + Solution obj = new Solution(); + ArrayList res = obj.fizzBuzz(n); + StringArray.print(res); + System.out.println("~"); + } + } +} + +class Solution { + public static ArrayList fizzBuzz(int n) { + ArrayList result = new ArrayList<>(); + HashMap mp = new HashMap<>(); + mp.put(3, "Fizz"); + mp.put(5, "Buzz"); + + int[] divisors = { 3, 5 }; + + for (int i = 1; i <= n; i++) { + StringBuilder s = new StringBuilder(); + for (int d : divisors) { + if (i % d == 0) { + s.append(mp.get(d)); + } + } + if (s.length() == 0) { + s.append(i); + } + result.add(s.toString()); + } + return result; + } +}