Skip to content

Commit

Permalink
Create Fizz Buzz
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Dec 5, 2024
1 parent 6578005 commit e37aa60
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Fizz Buzz
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> res = obj.fizzBuzz(n);
StringArray.print(res);
System.out.println("~");
}
}
}

class Solution {
public static ArrayList<String> fizzBuzz(int n) {
ArrayList<String> result = new ArrayList<>();
HashMap<Integer, String> 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;
}
}

0 comments on commit e37aa60

Please sign in to comment.