Here is the Before Java 8, simple solution:
public static void main(String[] args){ for(int i= 1; i <= 20; i++){ if(i % 15 == 0){ System.out.println("FizzBuzz"); }else if(i % 3 == 0){ System.out.println("Fizz"); }else if(i % 5 == 0){ System.out.println("Buzz"); }else{ System.out.println(i); } } }
Now, Using Stream API, here different methods to display solved fizzbuzz solution,
Method1:
public static void main(String[] args) { fizzBuzzDisplay(1, 20; } private static void fizzBuzzDisplay(int i, int j) { // TODO Auto-generated method stub IntStream.rangeClosed(i,j) .mapToObj(Test::transformNr) .forEach(System.out::println); }
Method 2:
public static void main(String[] args) { // TODO Auto-generated method stub IntStream.rangeClosed(1, 20).mapToObj(num -> getWordForNum(num).orElse(Integer.toString(num))) .forEach(System.out::println); } private static Optional<String> getWordForNum(int num) { String word = ""; if (isDiv(3).test(num)) word += "Fizz"; if (isDiv(5).test(num)) word += "Buzz"; return "".equals(word) ? Optional.empty() : Optional.of(word); } private static IntPredicate isDiv(int factor) { return arg -> (arg % factor) == 0; }
Method 3:
public static void main(String[] args) { // TODO Auto-generated method stub IntStream.rangeClosed(1, 20).mapToObj(A::fizzBuzz).forEach(System.out::println); } public static String fizzBuzz(int number) { if (number % 3==0 && number % 5 == 0) { return "FizzBuzz"; } else if (number % 3 == 0) { return "Fizz"; } else if (number % 5 == 0) { return "Buzz"; } return Integer.toString(number); }
Method 4:
public static void main(String[] args) { // TODO Auto-generated method stub IntStream.range(1, 20) .mapToObj(n -> { if (n % 15 == 0) return "FizzBuzz"; else if (n % 3 == 0) return "Fizz"; else if (n % 5 == 0) return "Buzz"; else return n; }).forEach(System.out::println); }
Mehtod 5:
public static void main(String[] args) { // TODO Auto-generated method stub IntStream.range(1, 20) .boxed() .map(x -> x+": " + (x%3==0? "Fizz": "") + (x%5==0? "Buzz": "")) .forEach(System.out::println); }
Output:
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
Happy Coding !!!
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Training in Chennai Core Java 8 Training in Chennai Core Java 8 Training in Chennai JavaEE Training in Chennai Java EE Training in Chennai
Nice One..
ReplyDeleteiso 27001 certification services
ReplyDeleteiso 27001 certification in delhi
ISO 9001 Certification in Noida
iso 22000 certification in Delhi
iso certification in noida
ReplyDeleteiso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida
we have provide the best fridge repair service.
ReplyDeleteWashing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Videocon Washing Machine Service Centre In Faridabad
IFB Washing Machine service centre in faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida
we have provide the best ppc service.
ReplyDeleteppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company
Rice Bags Manufacturers
ReplyDeletePouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
seed bag manufacturers
gusseted bag manufacturers
bopp laminated bags manufacturer
Lyrics with music
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai