FizzBuzz Test
“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”.
# Create counter to check first 100 numbers
n=100
counter = NULL
counter.output = NULL
counter = seq(1, n, by=1)
# Generalize for any varaible a & b
a=3
b=5
for (i in 1:n){
if(counter[i] %% a == 0 & counter[i] %% b == 0) {
counter.output[i] = "FizzBuzz"
}
else if (counter[i] %% a == 0) {
counter.output[i] = "Fizz"
}
else if (counter[i] %% b == 0) {
counter.output[i] = "Buzz"
}
else counter.output[i] = counter[i]
}
Output Table to Search
# create data frame
d<-data.frame(as.character(counter), counter.output)
colnames(d)<- c("Number", "FizzBuzz")
# Output Table
DT::datatable(d, rownames=FALSE, options = list(autowidth=TRUE,sClass="alignRight", className = 'dt-center', pageLength=10))