Print string depending on number
Sorry to bother you about this, but I had an interview months ago where they asked me to program the following in Java:

For numbers from 1 to 100 do:

 - when the number is multiple of 3 it should print 'Fizz'
 - When the number is multiple of 5 it should print 'Buzz'
 - When the number is multiple of 3 and 5 it should print 'FizzBuzz'
 
So I did the following:

    Public Class MyClass{
           public static void main (String args[]){
             for(int i=1; i<=100; i++){
              System.out.println("Number: " +i+ "," + FizzOrBuzz(i));
              }
           }
    
           public startic String FizzOrBuzz(int num){
                String fizz = (num % 3 ==0)? "Fizz":"";
                String buzz = (num % 5 ==0)? "Buzz":"";
                return fizz+buzz;
           }
    }

They asked me then: "How would it be more efficient?" And I say I could use StringBuffer instead of String, and even change the int for byte, but that didn't convinced them and they rejected me. 

So, after a few months I'm still thinking how could I make it more efficient. I know I could make it recursive but is it really more efficient? isn't more efficient iteration?

What I'm asking is the most efficient way to solve this problem. Can anyone help me here?

Thanks in advance!



Print string depending on number
===

Sorry to bother you about this, but I had an interview months ago where they asked me to program the following in Java:

For numbers from 1 to 100 do:

 - when the number is multiple of 3 it should print 'Fizz'
 - When the number is multiple of 5 it should print 'Buzz'
 - When the number is multiple of 3 and 5 it should print 'FizzBuzz'
 
So I did the following:

    Public Class MyClass{
           public static void main (String args[]){
             for(int i=1; i<=100; i++){
              System.out.println("Number: " +i+ "," + FizzOrBuzz(i));
              }
           }
    
           public startic String FizzOrBuzz(int num){
                String fizz = (num % 3 ==0)? "Fizz":"";
                String buzz = (num % 5 ==0)? "Buzz":"";
                return fizz+buzz;
           }
    }

They asked me then: "How would it be more efficient?" And I say I could use StringBuffer instead of String, and even change the int for byte, but that didn't convinced them and they rejected me. 

So, after a few months I'm still thinking how could I make it more efficient. I know I could make it recursive but is it really more efficient? isn't more efficient iteration?

What I'm asking is the most efficient way to solve this problem. Can anyone help me here?

Thanks in advance!


