CodingHorror Blog ( http://www.codinghorror.com/blog/archives/000781.html ) quotes the problem concerning the lack of competent programmers trying to obtain jobs very nicely,
"Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution." - Jeff Atwood
Now its your turn to try it! Here are the rules:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"." -CodingHorror.com
public class FizzBuzz {
public static string fizzbuzz(int i) {
if(i % 3 == 0 && i %  5 == 0) { // or i % 15 == 0
return "FizzBuzz";
}
else if(i % 3 == 0) {
return "Fizz";
}
else if(i % 5 == 0) {
return "Buzz";
}
else return Integer.toString(i);
}
public static void main(string[] args) {
for(int i = 1; i <= 100; i++) {
System.out.println(fizzbuzz(i));
}
}
}
0 comments:
Post a Comment