diff --git a/Java/FizzBuzz_ramgsuri.java b/Java/FizzBuzz_ramgsuri.java new file mode 100644 index 0000000..91143cb --- /dev/null +++ b/Java/FizzBuzz_ramgsuri.java @@ -0,0 +1,17 @@ +import java.util.stream.IntStream; + +public class FizzBuzz_ramgsuri { + + public static void main(String args[]){ + + IntStream.rangeClosed(1, 100) + .mapToObj(ifNumberMod(15, "FizzBuzz", + ifNumberMod(5, "Buzz", + ifNumberMod(3, "Fizz", Integer::toString)))) + .forEach(System.out::println); + } + + static IntFunction ifNumberMod(int m, R r, IntFunction f) { + return (int i) -> (i % m == 0) ? r : f.apply(i); + } +} diff --git a/Java/FizzBuzzv2.java b/Java/FizzBuzzv2.java index 50d5703..23ca005 100644 --- a/Java/FizzBuzzv2.java +++ b/Java/FizzBuzzv2.java @@ -12,11 +12,11 @@ private static void fizzBuzzBeforeJava8(int num) { for (int i = 1; i <= num; i++) { - if (((i % 5) == 0) && ((i % 7) == 0)) // Is it a multiple of 5 & 7? + if (((i % 5) == 0) && ((i % 3) == 0)) // Is it a multiple of 5 & 7? System.out.println("fizzbuzz"); - else if ((i % 5) == 0) // Is it a multiple of 5? + else if ((i % 3) == 0) // Is it a multiple of 5? System.out.println("fizz"); - else if ((i % 7) == 0) // Is it a multiple of 7? + else if ((i % 5) == 0) // Is it a multiple of 7? System.out.println("buzz"); else System.out.println(i); // Not a multiple of 5 or 7 @@ -25,7 +25,7 @@ else if ((i % 7) == 0) // Is it a multiple of 7? private static void fizzBuzzInJava8(int num) { IntStream.rangeClosed(1, 100) - .mapToObj(i -> i % 5 == 0 ? (i % 7 == 0 ? "FizzBuzz" : "Fizz") : (i % 7 == 0 ? "Buzz" : i)) + .mapToObj(i -> i % 3 == 0 ? (i % 5 == 0 ? "FizzBuzz" : "Fizz") : (i % 5 == 0 ? "Buzz" : i)) .forEach(System.out::println); } } \ No newline at end of file