Given a number n, for each integer i in the range
from 1 to n inclusive, print one value per line as
follows:
• Ifi is a multiple of both 3 and 5, print FizzBuzz.
• If iis a multiple of 3(but not 5), print Fizz.
• Ifi is a multiple of 5(but not 3), print Buzz.
• If i is not a multiple of 3 or 5, print the value of i. write a program in Java
Answers
Answer:
FizzBuzz: Given an integer number n, for multiples of three print “Fizz” for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Raw
FizzBuzz.c
#include <stdio.h>
void fizz_buzz(int n)
{
int i;
for (i=1; i<=n; i++) {
if (i%3==0) printf("Fizz");
if (i%5==0) printf("Buzz");
if ((i%3)*(i%5)) printf("%d", i);
printf("\n");
}
}
Raw
FizzBuzz.go
package main
import "fmt"
type fizzbuzz int
func (x fizzbuzz) String() string {
switch [2]bool{x%3 == 0, x%5 == 0} {
case [2]bool{true, true}:
return "FizzBuzz"
case [2]bool{true, false}:
return "Fizz"
case [2]bool{false, true}:
return "Buzz"
default:
return fmt.Sprint(int(x))
}
}
func main() {
for x := fizzbuzz(1); x <= 100; x++ {
fmt.Println(x)
}
}
Raw
FizzBuzz.js
for (var i=1; i<=100; i++) {
if (i%15===0) {
console.log("FizzBuzz");
} else if (i%3===0) {
console.log("Fizz");
} else if (i%5===0) {
console.log("Buzz");
} else {
console.log(i)
}
}
Raw
FizzBuzz.py
def fizz_buzz(n):
return [(not n % 3) * 'Fizz' + (not n % 5) * 'Buzz' or n for n in range(1, 101)]
Raw
FizzBuzz.swift
func fizzBuzz(n: Int) -> String {
let result = (n%3, n%5)
switch result {
case (0, _):
return "Fizz"
case (_, 0):
return "Buzz"
case (0, 0):
return "FizzBuzz"
default:
return "\(n)"
}
}