Thursday, June 28, 2018

Fizz Buzz

Problem statement: 
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

import java.util.List;
class Solution {
    public static List<String> fizzBuzz(int n) {
        List<String> str = null;
        for (int i =1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 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);
            }
            //return str;        }
        return str;
    }
    public static void  main(String args[]){
        fizzBuzz(15);
    }

Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...