So I read about simple test given in interviews to applicants for programming positions. Write a program that prints the integers i from 1 to 100 with the following exceptions:

- If i is a multiple of 3 () print “Fizz”;
- If i is a multiple of 5 (), print “Buzz”;
- If i is a multiple of both 3 and 5 (), print “FizzBuzz”.

Apparently, a large percentage of applicants struggle with this task. I guess I did as well: I couldn’t remember the arithmetic operator for modular arithmetic, and I omitted a logical condition in my first draft. I am not a programmer, and I do not know many programming languages; but I am becoming pretty familiar with R, so I wrote an R script that does as desired above. Here’s my code:

for (i in 1:100) {
if (((i %% 3) == 0) & ((i %% 5) > 0)) {
print("Fizz")
} else if (((i %% 5) == 0) & ((i %% 3) > 0)) {
print("Buzz")
} else if ((i %% 15) == 0) {
print("FizzBuzz")
} else {
print(i)
}
}

### Like this:

Like Loading...

*Related*