Syntax Highlighting
Syntax Highlighting Test – 55+ Programming Languages
This file contains code blocks for 55 programming languages. Each snippet includes:
- A variable declaration
- A function that implements FizzBuzz (prints or returns the result for numbers 1–15)
- A simple class definition (if the language supports classes)
Languages are ordered alphabetically.
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure FizzBuzz is
-- Variable declaration
Message : constant String := "Hello, Ada!";
-- Function (FizzBuzz)
function Eval(N : Integer) return String is
begin
if N mod 15 = 0 then
return "FizzBuzz";
elsif N mod 3 = 0 then
return "Fizz";
elsif N mod 5 = 0 then
return "Buzz";
else
return Integer'Image(N);
end if;
end Eval;
-- Class-like (Ada uses packages)
package Greeter is
procedure Say_Hello;
end Greeter;
package body Greeter is
procedure Say_Hello is
begin
Put_Line(Message);
end Say_Hello;
end Greeter;
begin
Greeter.Say_Hello;
for I in 1..15 loop
Put_Line(Eval(I));
end loop;
end FizzBuzz;
APL
⍝ Variable declaration
message ← 'Hello, APL!'
⍝ FizzBuzz function
FizzBuzz ← {
⍵=0: ''
(3 5 ∊⍨ 3 5|⍵) / 'Fizz' 'Buzz'
((0=15|⍵) ∨ (0≠3|⍵) ∧ (0≠5|⍵)) / ⍕⍵
}
⍝ No classes in traditional APL
∇ Test
⎕ ← message
FizzBuzz ¨ ⍳15
∇
Test
Assembly (x86)
section .data
; Variable declaration
message db 'Hello, Assembly!', 0xa
len equ $ - message
section .text
global _start
; Function: fizzbuzz (simplified – prints numbers 1-15 with FizzBuzz logic)
fizzbuzz:
push ebp
mov ebp, esp
mov ecx, 1
.loop:
cmp ecx, 16
jge .done
; Check divisibility (omitted for brevity – would print string)
; For testing, just print the number
push ecx
call print_num
pop ecx
inc ecx
jmp .loop
.done:
pop ebp
ret
print_num:
; (implementation omitted)
ret
_start:
; Print message
mov eax, 4
mov ebx, 1
mov ecx, message
mov edx, len
int 0x80
; Call fizzbuzz
call fizzbuzz
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
Bash
#!/bin/bash
# Variable declaration
message="Hello, Bash!"
# FizzBuzz function
fizzbuzz() {
local n=$1
if (( n % 15 == 0 )); then
echo "FizzBuzz"
elif (( n % 3 == 0 )); then
echo "Fizz"
elif (( n % 5 == 0 )); then
echo "Buzz"
else
echo $n
fi
}
# No classes in Bash
# Test
echo $message
for i in {1..15}; do
fizzbuzz $i
done
C
#include <stdio.h>
// Variable declaration
const char* message = "Hello, C!";
// FizzBuzz function
void fizzbuzz(int n) {
if (n % 15 == 0)
printf("FizzBuzz\n");
else if (n % 3 == 0)
printf("Fizz\n");
else if (n % 5 == 0)
printf("Buzz\n");
else
printf("%d\n", n);
}
// No classes in C
int main() {
printf("%s\n", message);
for (int i = 1; i <= 15; i++)
fizzbuzz(i);
return 0;
}
C#
using System;
class FizzBuzzExample
{
// Variable
static string message = "Hello, C#!";
// FizzBuzz function
static string FizzBuzz(int n)
{
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return n.ToString();
}
// Class
class Greeter
{
public void SayHello()
{
Console.WriteLine(message);
}
}
static void Main()
{
Greeter greeter = new Greeter();
greeter.SayHello();
for (int i = 1; i <= 15; i++)
Console.WriteLine(FizzBuzz(i));
}
}
C++
#include <iostream>
#include <string>
// Variable
const std::string message = "Hello, C++!";
// FizzBuzz function
std::string fizzbuzz(int n) {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return std::to_string(n);
}
// Class
class Greeter {
public:
void sayHello() {
std::cout << message << std::endl;
}
};
int main() {
Greeter g;
g.sayHello();
for (int i = 1; i <= 15; ++i)
std::cout << fizzbuzz(i) << std::endl;
return 0;
}
Clojure
;; Variable
(def message "Hello, Clojure!")
;; FizzBuzz function
(defn fizzbuzz [n]
(cond
(zero? (mod n 15)) "FizzBuzz"
(zero? (mod n 3)) "Fizz"
(zero? (mod n 5)) "Buzz"
:else (str n)))
;; Class-like via defrecord
(defrecord Greeter [msg]
(speak [this] (println msg)))
;; Test
(println message)
(doseq [i (range 1 16)]
(println (fizzbuzz i)))
((->Greeter message) :speak)
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. FizzBuzz.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MESSAGE PIC X(20) VALUE 'Hello, COBOL!'.
01 I PIC 99.
01 RESULT PIC X(8).
PROCEDURE DIVISION.
DISPLAY MESSAGE
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 15
PERFORM FIZZBUZZ
DISPLAY RESULT
END-PERFORM
STOP RUN.
FIZZBUZZ.
IF FUNCTION MOD(I, 15) = 0
MOVE 'FizzBuzz' TO RESULT
ELSE IF FUNCTION MOD(I, 3) = 0
MOVE 'Fizz' TO RESULT
ELSE IF FUNCTION MOD(I, 5) = 0
MOVE 'Buzz' TO RESULT
ELSE
MOVE I TO RESULT
END-IF.
CoffeeScript
# Variable
message = "Hello, CoffeeScript!"
# FizzBuzz function
fizzbuzz = (n) ->
if n % 15 == 0 then "FizzBuzz"
else if n % 3 == 0 then "Fizz"
else if n % 5 == 0 then "Buzz"
else n
# Class
class Greeter
constructor: (@msg) ->
speak: -> console.log @msg
# Test
console.log message
for i in [1..15]
console.log fizzbuzz i
greeter = new Greeter message
greeter.speak()
Crystal
# Variable
message = "Hello, Crystal!"
# FizzBuzz function
def fizzbuzz(n)
if n % 15 == 0
"FizzBuzz"
elsif n % 3 == 0
"Fizz"
elsif n % 5 == 0
"Buzz"
else
n.to_s
end
end
# Class
class Greeter
def initialize(@msg : String)
end
def speak
puts @msg
end
end
# Test
puts message
(1..15).each { |i| puts fizzbuzz(i) }
Greeter.new(message).speak
D
import std.stdio;
// Variable
string message = "Hello, D!";
// FizzBuzz function
string fizzbuzz(int n) {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return n.to!string;
}
// Class
class Greeter {
string msg;
this(string msg) { this.msg = msg; }
void speak() { writeln(msg); }
}
void main() {
writeln(message);
foreach (i; 1..16) writeln(fizzbuzz(i));
auto g = new Greeter(message);
g.speak();
}
Dart
// Variable
String message = "Hello, Dart!";
// FizzBuzz function
String fizzbuzz(int n) {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return n.toString();
}
// Class
class Greeter {
String msg;
Greeter(this.msg);
void speak() => print(msg);
}
void main() {
print(message);
for (int i = 1; i <= 15; i++) {
print(fizzbuzz(i));
}
Greeter(message).speak();
}
Dockerfile (not a programming language, but included for highlighting)
# Variable (ARG)
ARG message="Hello, Docker!"
# FizzBuzz function? Not applicable; instead we show a multi-stage build
FROM alpine:latest AS builder
RUN echo "Building..."
FROM alpine:latest
COPY --from=builder /some/file /app/
CMD ["echo", "FizzBuzz not applicable in Dockerfile"]
Elixir
# Variable
message = "Hello, Elixir!"
# FizzBuzz function
fizzbuzz = fn n ->
cond do
rem(n, 15) == 0 -> "FizzBuzz"
rem(n, 3) == 0 -> "Fizz"
rem(n, 5) == 0 -> "Buzz"
true -> Integer.to_string(n)
end
end
# Class-like (module)
defmodule Greeter do
def speak(msg) do
IO.puts(msg)
end
end
# Test
IO.puts(message)
Enum.each(1..15, fn i -> IO.puts(fizzbuzz.(i)) end)
Greeter.speak(message)
Elm
module Main exposing (..)
import Html exposing (text)
-- Variable
message = "Hello, Elm!"
-- FizzBuzz function
fizzbuzz n =
if modBy 15 n == 0 then "FizzBuzz"
else if modBy 3 n == 0 then "Fizz"
else if modBy 5 n == 0 then "Buzz"
else String.fromInt n
-- No classes; use records
type alias Greeter = { msg : String }
greeter = { msg = message }
main = text (greeter.msg ++ " " ++ (fizzbuzz 3))
Erlang
-module(fizzbuzz).
-export([start/0, fizzbuzz/1]).
% Variable
message() -> "Hello, Erlang!".
% FizzBuzz function
fizzbuzz(N) when N rem 15 == 0 -> "FizzBuzz";
fizzbuzz(N) when N rem 3 == 0 -> "Fizz";
fizzbuzz(N) when N rem 5 == 0 -> "Buzz";
fizzbuzz(N) -> integer_to_list(N).
% Class-like via module
start() ->
io:format("~s~n", [message()]),
[io:format("~s~n", [fizzbuzz(I)]) || I <- lists:seq(1,15)].
F#
// Variable
let message = "Hello, F#!"
// FizzBuzz function
let fizzbuzz n =
match n with
| n when n % 15 = 0 -> "FizzBuzz"
| n when n % 3 = 0 -> "Fizz"
| n when n % 5 = 0 -> "Buzz"
| _ -> string n
// Class
type Greeter(msg: string) =
member this.Speak() = printfn "%s" msg
// Test
printfn "%s" message
[1..15] |> List.iter (fizzbuzz >> printfn "%s")
let greeter = Greeter(message)
greeter.Speak()
Fortran
program fizzbuzz
implicit none
character(len=20) :: message
integer :: i
! Variable
message = "Hello, Fortran!"
! Print message
print *, message
! FizzBuzz loop
do i = 1, 15
print *, fizzbuzz_func(i)
end do
contains
! FizzBuzz function
function fizzbuzz_func(n) result(res)
integer, intent(in) :: n
character(len=8) :: res
if (mod(n, 15) == 0) then
res = "FizzBuzz"
else if (mod(n, 3) == 0) then
res = "Fizz"
else if (mod(n, 5) == 0) then
res = "Buzz"
else
write(res, '(I0)') n
end if
end function fizzbuzz_func
end program fizzbuzz
Go
package main
import "fmt"
// Variable
var message = "Hello, Go!"
// FizzBuzz function
func fizzbuzz(n int) string {
switch {
case n%15 == 0:
return "FizzBuzz"
case n%3 == 0:
return "Fizz"
case n%5 == 0:
return "Buzz"
default:
return fmt.Sprintf("%d", n)
}
}
// Class-like (struct + method)
type Greeter struct {
msg string
}
func (g Greeter) speak() {
fmt.Println(g.msg)
}
func main() {
fmt.Println(message)
for i := 1; i <= 15; i++ {
fmt.Println(fizzbuzz(i))
}
g := Greeter{msg: message}
g.speak()
}
Groovy
// Variable
def message = "Hello, Groovy!"
// FizzBuzz function
def fizzbuzz(n) {
if (n % 15 == 0) "FizzBuzz"
else if (n % 3 == 0) "Fizz"
else if (n % 5 == 0) "Buzz"
else n
}
// Class
class Greeter {
String msg
Greeter(msg) { this.msg = msg }
void speak() { println msg }
}
// Test
println message
(1..15).each { println fizzbuzz(it) }
new Greeter(message).speak()
Haskell
-- Variable
message = "Hello, Haskell!"
-- FizzBuzz function
fizzbuzz n
| n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 3 == 0 = "Fizz"
| n `mod` 5 == 0 = "Buzz"
| otherwise = show n
-- Class-like (typeclass + instance)
class Speaker a where
speak :: a -> IO ()
data Greeter = Greeter String
instance Speaker Greeter where
speak (Greeter msg) = putStrLn msg
main :: IO ()
main = do
putStrLn message
mapM_ (putStrLn . fizzbuzz) [1..15]
speak (Greeter message)
HTML (not a programming language, but for highlighting)
<!DOCTYPE html>
<html>
<head>
<title>FizzBuzz Test</title>
</head>
<body>
<!-- Variable via data attribute -->
<div id="message" data-msg="Hello, HTML!">Hello, HTML!</div>
<script>
// FizzBuzz in JavaScript embedded
const msg = document.getElementById('message').dataset.msg;
console.log(msg);
for (let i = 1; i <= 15; 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);
}
</script>
</body>
</html>
Java
public class FizzBuzzExample {
// Variable
static String message = "Hello, Java!";
// FizzBuzz function
static String fizzbuzz(int n) {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return String.valueOf(n);
}
// Class
static class Greeter {
String msg;
Greeter(String msg) { this.msg = msg; }
void speak() { System.out.println(msg); }
}
public static void main(String[] args) {
System.out.println(message);
for (int i = 1; i <= 15; i++)
System.out.println(fizzbuzz(i));
new Greeter(message).speak();
}
}
JavaScript
// Variable
const message = "Hello, JavaScript!";
// FizzBuzz function
function fizzbuzz(n) {
if (n % 15 === 0) return "FizzBuzz";
if (n % 3 === 0) return "Fizz";
if (n % 5 === 0) return "Buzz";
return n;
}
// Class
class Greeter {
constructor(msg) { this.msg = msg; }
speak() { console.log(this.msg); }
}
// Test
console.log(message);
for (let i = 1; i <= 15; i++) console.log(fizzbuzz(i));
new Greeter(message).speak();
Julia
# Variable
message = "Hello, Julia!"
# FizzBuzz function
function fizzbuzz(n)
if n % 15 == 0
"FizzBuzz"
elseif n % 3 == 0
"Fizz"
elseif n % 5 == 0
"Buzz"
else
string(n)
end
end
# Class-like (struct + method)
struct Greeter
msg::String
end
speak(g::Greeter) = println(g.msg)
# Test
println(message)
for i in 1:15
println(fizzbuzz(i))
end
speak(Greeter(message))
Kotlin
// Variable
val message = "Hello, Kotlin!"
// FizzBuzz function
fun fizzbuzz(n: Int): String = when {
n % 15 == 0 -> "FizzBuzz"
n % 3 == 0 -> "Fizz"
n % 5 == 0 -> "Buzz"
else -> n.toString()
}
// Class
class Greeter(val msg: String) {
fun speak() = println(msg)
}
// Test
fun main() {
println(message)
for (i in 1..15) println(fizzbuzz(i))
Greeter(message).speak()
}
Less
// Variable
@message: "Hello, Less!";
// FizzBuzz mixin (function-like)
.fizzbuzz(@n) when (@n % 15 = 0) { content: "FizzBuzz"; }
.fizzbuzz(@n) when (@n % 3 = 0) and (@n % 15 != 0) { content: "Fizz"; }
.fizzbuzz(@n) when (@n % 5 = 0) and (@n % 15 != 0) { content: "Buzz"; }
.fizzbuzz(@n) when (@n % 3 != 0) and (@n % 5 != 0) { content: @n; }
// Class-like (ruleset)
.greeter {
&:after {
content: @message;
}
}
// Usage
body {
.greeter;
.fizzbuzz(3);
}
Lisp (Common Lisp)
;; Variable
(defparameter *message* "Hello, Lisp!")
;; FizzBuzz function
(defun fizzbuzz (n)
(cond ((= (mod n 15) 0) "FizzBuzz")
((= (mod n 3) 0) "Fizz")
((= (mod n 5) 0) "Buzz")
(t (write-to-string n))))
;; Class-like (CLOS)
(defclass greeter ()
((msg :initarg :msg :accessor msg)))
(defmethod speak ((g greeter))
(format t "~a~%" (msg g)))
;; Test
(format t "~a~%" *message*)
(loop for i from 1 to 15 do (format t "~a~%" (fizzbuzz i)))
(speak (make-instance 'greeter :msg *message*))
Lua
-- Variable
local message = "Hello, Lua!"
-- FizzBuzz function
function fizzbuzz(n)
if n % 15 == 0 then return "FizzBuzz"
elseif n % 3 == 0 then return "Fizz"
elseif n % 5 == 0 then return "Buzz"
else return tostring(n) end
end
-- Class-like (table + metatable)
Greeter = {}
function Greeter:new(msg)
local obj = {msg = msg}
setmetatable(obj, self)
self.__index = self
return obj
end
function Greeter:speak()
print(self.msg)
end
-- Test
print(message)
for i = 1, 15 do print(fizzbuzz(i)) end
local g = Greeter:new(message)
g:speak()
Makefile
# Variable
MESSAGE := Hello, Make!
# FizzBuzz function (not really, just a target)
.PHONY: fizzbuzz
fizzbuzz:
@for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do \
if [ $$((i % 15)) -eq 0 ]; then echo "FizzBuzz"; \
elif [ $$((i % 3)) -eq 0 ]; then echo "Fizz"; \
elif [ $$((i % 5)) -eq 0 ]; then echo "Buzz"; \
else echo $$i; fi \
done
# Class-like not applicable; use target
greeter:
@echo $(MESSAGE)
all: greeter fizzbuzz
Markdown (not a language, but for highlighting)
---
message: "Hello, Markdown!"
---
# FizzBuzz Example
Variable: {{ message }}
For numbers 1 to 15:
- 1
- 2
- Fizz
- 4
- Buzz
- Fizz
- 7
- 8
- Fizz
- Buzz
- 11
- Fizz
- 13
- 14
- FizzBuzz
Nim
# Variable
let message = "Hello, Nim!"
# FizzBuzz function
proc fizzbuzz(n: int): string =
if n mod 15 == 0: "FizzBuzz"
elif n mod 3 == 0: "Fizz"
elif n mod 5 == 0: "Buzz"
else: $n
# Class-like (object + method)
type Greeter = object
msg: string
proc speak(g: Greeter) = echo g.msg
# Test
echo message
for i in 1..15: echo fizzbuzz(i)
let g = Greeter(msg: message)
g.speak()
Objective-C
#import <Foundation/Foundation.h>
// Variable
NSString *message = @"Hello, Objective-C!";
// FizzBuzz function
NSString* fizzbuzz(int n) {
if (n % 15 == 0) return @"FizzBuzz";
if (n % 3 == 0) return @"Fizz";
if (n % 5 == 0) return @"Buzz";
return [NSString stringWithFormat:@"%d", n];
}
// Class
@interface Greeter : NSObject
@property NSString *msg;
- (void)speak;
@end
@implementation Greeter
- (void)speak {
NSLog(@"%@", self.msg);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"%@", message);
for (int i = 1; i <= 15; i++) {
NSLog(@"%@", fizzbuzz(i));
}
Greeter *g = [[Greeter alloc] init];
g.msg = message;
[g speak];
}
return 0;
}
OCaml
(* Variable *)
let message = "Hello, OCaml!"
(* FizzBuzz function *)
let fizzbuzz n =
if n mod 15 = 0 then "FizzBuzz"
else if n mod 3 = 0 then "Fizz"
else if n mod 5 = 0 then "Buzz"
else string_of_int n
(* Class-like (module) *)
module Greeter = struct
let speak msg = print_endline msg
end
(* Test *)
let () =
print_endline message;
for i = 1 to 15 do
print_endline (fizzbuzz i)
done;
Greeter.speak message
Pascal
program FizzBuzz;
// Variable
var
message: string;
// FizzBuzz function
function fizzbuzz(n: integer): string;
begin
if n mod 15 = 0 then
fizzbuzz := 'FizzBuzz'
else if n mod 3 = 0 then
fizzbuzz := 'Fizz'
else if n mod 5 = 0 then
fizzbuzz := 'Buzz'
else
str(n, fizzbuzz);
end;
// No classes; use record
type
Greeter = record
msg: string;
end;
procedure speak(g: Greeter);
begin
writeln(g.msg);
end;
var
i: integer;
g: Greeter;
begin
message := 'Hello, Pascal!';
writeln(message);
for i := 1 to 15 do
writeln(fizzbuzz(i));
g.msg := message;
speak(g);
end.
Perl
# Variable
my $message = "Hello, Perl!";
# FizzBuzz function
sub fizzbuzz {
my $n = shift;
if ($n % 15 == 0) { return "FizzBuzz"; }
elsif ($n % 3 == 0) { return "Fizz"; }
elsif ($n % 5 == 0) { return "Buzz"; }
else { return $n; }
}
# Class (package)
package Greeter;
sub new { bless { msg => shift }, shift; }
sub speak { my $self = shift; print "$self->{msg}\n"; }
package main;
# Test
print "$message\n";
for my $i (1..15) { print fizzbuzz($i), "\n"; }
my $g = Greeter->new($message);
$g->speak();
PHP
<?php
// Variable
$message = "Hello, PHP!";
// FizzBuzz function
function fizzbuzz($n) {
if ($n % 15 == 0) return "FizzBuzz";
if ($n % 3 == 0) return "Fizz";
if ($n % 5 == 0) return "Buzz";
return (string)$n;
}
// Class
class Greeter {
private $msg;
public function __construct($msg) { $this->msg = $msg; }
public function speak() { echo $this->msg . "\n"; }
}
// Test
echo $message . "\n";
for ($i = 1; $i <= 15; $i++) {
echo fizzbuzz($i) . "\n";
}
$g = new Greeter($message);
$g->speak();
?>
PowerShell
# Variable
$message = "Hello, PowerShell!"
# FizzBuzz function
function fizzbuzz($n) {
if ($n % 15 -eq 0) { "FizzBuzz" }
elseif ($n % 3 -eq 0) { "Fizz" }
elseif ($n % 5 -eq 0) { "Buzz" }
else { $n }
}
# Class (PowerShell 5+)
class Greeter {
[string]$msg
Greeter([string]$msg) { $this.msg = $msg }
[void]speak() { Write-Host $this.msg }
}
# Test
$message
1..15 | ForEach-Object { fizzbuzz $_ }
$g = [Greeter]::new($message)
$g.speak()
Prolog
% Variable
message('Hello, Prolog!').
% FizzBuzz predicate
fizzbuzz(N, 'FizzBuzz') :- 0 is N mod 15.
fizzbuzz(N, 'Fizz') :- 0 is N mod 3.
fizzbuzz(N, 'Buzz') :- 0 is N mod 5.
fizzbuzz(N, N) :- number(N).
% Class-like (module with predicate)
speak(Msg) :- write(Msg), nl.
% Test
:- initialization(main).
main :-
message(M),
speak(M),
forall(between(1,15,N), (fizzbuzz(N,R), write(R), nl)).
Python
# Variable
message = "Hello, Python!"
# FizzBuzz function
def fizzbuzz(n):
if n % 15 == 0:
return "FizzBuzz"
if n % 3 == 0:
return "Fizz"
if n % 5 == 0:
return "Buzz"
return str(n)
# Class
class Greeter:
def __init__(self, msg):
self.msg = msg
def speak(self):
print(self.msg)
# Test
print(message)
for i in range(1, 16):
print(fizzbuzz(i))
g = Greeter(message)
g.speak()
R
# Variable
message <- "Hello, R!"
# FizzBuzz function
fizzbuzz <- function(n) {
if (n %% 15 == 0) "FizzBuzz"
else if (n %% 3 == 0) "Fizz"
else if (n %% 5 == 0) "Buzz"
else as.character(n)
}
# Class (S3)
greeter <- function(msg) {
structure(list(msg = msg), class = "greeter")
}
speak.greeter <- function(x) {
print(x$msg)
}
# Test
print(message)
for (i in 1:15) print(fizzbuzz(i))
g <- greeter(message)
speak(g)
Racket
#lang racket
;; Variable
(define message "Hello, Racket!")
;; FizzBuzz function
(define (fizzbuzz n)
(cond [(= (modulo n 15) 0) "FizzBuzz"]
[(= (modulo n 3) 0) "Fizz"]
[(= (modulo n 5) 0) "Buzz"]
[else (number->string n)]))
;; Class-like (struct)
(struct greeter (msg) #:methods gen:custom-write
[(define (speak g) (displayln (greeter-msg g)))])
;; Test
(displayln message)
(for ([i (in-range 1 16)])
(displayln (fizzbuzz i)))
(speak (greeter message))
Ruby
# Variable
message = "Hello, Ruby!"
# FizzBuzz function
def fizzbuzz(n)
if n % 15 == 0
"FizzBuzz"
elsif n % 3 == 0
"Fizz"
elsif n % 5 == 0
"Buzz"
else
n.to_s
end
end
# Class
class Greeter
def initialize(msg)
@msg = msg
end
def speak
puts @msg
end
end
# Test
puts message
(1..15).each { |i| puts fizzbuzz(i) }
g = Greeter.new(message)
g.speak
Rust
// Variable
let message = "Hello, Rust!";
// FizzBuzz function
fn fizzbuzz(n: i32) -> String {
if n % 15 == 0 {
"FizzBuzz".to_string()
} else if n % 3 == 0 {
"Fizz".to_string()
} else if n % 5 == 0 {
"Buzz".to_string()
} else {
n.to_string()
}
}
// Class-like (struct + impl)
struct Greeter {
msg: String,
}
impl Greeter {
fn new(msg: &str) -> Self {
Greeter { msg: msg.to_string() }
}
fn speak(&self) {
println!("{}", self.msg);
}
}
fn main() {
println!("{}", message);
for i in 1..=15 {
println!("{}", fizzbuzz(i));
}
let g = Greeter::new(message);
g.speak();
}
Scala
object FizzBuzzExample {
// Variable
val message = "Hello, Scala!"
// FizzBuzz function
def fizzbuzz(n: Int): String = n match {
case x if x % 15 == 0 => "FizzBuzz"
case x if x % 3 == 0 => "Fizz"
case x if x % 5 == 0 => "Buzz"
case _ => n.toString
}
// Class
class Greeter(msg: String) {
def speak(): Unit = println(msg)
}
def main(args: Array[String]): Unit = {
println(message)
(1 to 15).foreach(i => println(fizzbuzz(i)))
new Greeter(message).speak()
}
}
Scheme
;; Variable
(define message "Hello, Scheme!")
;; FizzBuzz function
(define (fizzbuzz n)
(cond ((= (modulo n 15) 0) "FizzBuzz")
((= (modulo n 3) 0) "Fizz")
((= (modulo n 5) 0) "Buzz")
(else (number->string n))))
;; Class-like (using closures)
(define (make-greeter msg)
(lambda () (display msg) (newline)))
;; Test
(display message) (newline)
(do ((i 1 (+ i 1))) ((> i 15))
(display (fizzbuzz i)) (newline))
((make-greeter message))
Shell (sh)
#!/bin/sh
# Variable
message="Hello, Shell!"
# FizzBuzz function
fizzbuzz() {
n=$1
if [ $((n % 15)) -eq 0 ]; then
echo "FizzBuzz"
elif [ $((n % 3)) -eq 0 ]; then
echo "Fizz"
elif [ $((n % 5)) -eq 0 ]; then
echo "Buzz"
else
echo $n
fi
}
# No classes in sh
# Test
echo $message
i=1
while [ $i -le 15 ]; do
fizzbuzz $i
i=$((i + 1))
done
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract FizzBuzzExample {
// Variable
string public message = "Hello, Solidity!";
// FizzBuzz function
function fizzbuzz(uint n) public pure returns (string memory) {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return uint2str(n);
}
// Helper to convert uint to string
function uint2str(uint _i) internal pure returns (string memory) {
if (_i == 0) return "0";
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
// Class-like (another contract)
contract Greeter {
string private msg;
constructor(string memory _msg) { msg = _msg; }
function speak() public view returns (string memory) { return msg; }
}
// Test function
function test() public view returns (string memory) {
// Just return something; can't loop easily in pure/view
return fizzbuzz(15);
}
}
SQL
-- Variable (using a CTE)
WITH vars AS (
SELECT 'Hello, SQL!' AS message
),
-- FizzBuzz function (inline via CASE)
numbers AS (
SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
)
SELECT
n,
CASE
WHEN n % 15 = 0 THEN 'FizzBuzz'
WHEN n % 3 = 0 THEN 'Fizz'
WHEN n % 5 = 0 THEN 'Buzz'
ELSE CAST(n AS VARCHAR)
END AS result
FROM numbers, vars;
-- No classes in standard SQL
Swift
// Variable
let message = "Hello, Swift!"
// FizzBuzz function
func fizzbuzz(_ n: Int) -> String {
if n % 15 == 0 { return "FizzBuzz" }
if n % 3 == 0 { return "Fizz" }
if n % 5 == 0 { return "Buzz" }
return String(n)
}
// Class
class Greeter {
let msg: String
init(msg: String) { self.msg = msg }
func speak() { print(msg) }
}
// Test
print(message)
for i in 1...15 {
print(fizzbuzz(i))
}
let g = Greeter(msg: message)
g.speak()
TypeScript
// Variable
const message: string = "Hello, TypeScript!";
// FizzBuzz function
function fizzbuzz(n: number): string {
if (n % 15 === 0) return "FizzBuzz";
if (n % 3 === 0) return "Fizz";
if (n % 5 === 0) return "Buzz";
return n.toString();
}
// Class
class Greeter {
constructor(private msg: string) {}
speak(): void {
console.log(this.msg);
}
}
// Test
console.log(message);
for (let i = 1; i <= 15; i++) {
console.log(fizzbuzz(i));
}
new Greeter(message).speak();
VBA
Option Explicit
Sub FizzBuzz()
' Variable
Dim message As String
message = "Hello, VBA!"
' Print message
Debug.Print message
' FizzBuzz loop
Dim i As Integer
For i = 1 To 15
Debug.Print fizzbuzz(i)
Next i
' Class-like (using a separate class module would be required)
' For brevity, just call a sub
Speak message
End Sub
Function fizzbuzz(n As Integer) As String
If n Mod 15 = 0 Then
fizzbuzz = "FizzBuzz"
ElseIf n Mod 3 = 0 Then
fizzbuzz = "Fizz"
ElseIf n Mod 5 = 0 Then
fizzbuzz = "Buzz"
Else
fizzbuzz = CStr(n)
End If
End Function
Sub Speak(msg As String)
Debug.Print msg
End Sub
Verilog
module fizzbuzz;
// Variable
reg [31:0] message = "Hello, Verilog!";
integer i;
// FizzBuzz task (function-like)
task fizzbuzz;
input integer n;
begin
if (n % 15 == 0) $display("FizzBuzz");
else if (n % 3 == 0) $display("Fizz");
else if (n % 5 == 0) $display("Buzz");
else $display("%0d", n);
end
endtask
// No classes; just module
initial begin
$display("%s", message);
for (i = 1; i <= 15; i = i + 1) begin
fizzbuzz(i);
end
#10 $finish;
end
endmodule
VHDL
entity fizzbuzz is
end entity;
architecture sim of fizzbuzz is
-- Variable (in process)
begin
process is
variable message : string(1 to 15) := "Hello, VHDL! ";
variable i : integer := 1;
begin
report message;
for i in 1 to 15 loop
if i mod 15 = 0 then
report "FizzBuzz";
elsif i mod 3 = 0 then
report "Fizz";
elsif i mod 5 = 0 then
report "Buzz";
else
report integer'image(i);
end if;
end loop;
wait;
end process;
end architecture;
XML (not a language, but for highlighting)
<?xml version="1.0" encoding="UTF-8"?>
<fizzbuzz>
<!-- Variable -->
<variable name="message" value="Hello, XML!"/>
<!-- FizzBuzz function represented as elements -->
<numbers>
<number value="1">1</number>
<number value="2">2</number>
<number value="3">Fizz</number>
<number value="4">4</number>
<number value="5">Buzz</number>
<number value="6">Fizz</number>
<number value="7">7</number>
<number value="8">8</number>
<number value="9">Fizz</number>
<number value="10">Buzz</number>
<number value="11">11</number>
<number value="12">Fizz</number>
<number value="13">13</number>
<number value="14">14</number>
<number value="15">FizzBuzz</number>
</numbers>
<!-- Class-like (element with method) -->
<greeter>
<message>Hello, XML!</message>
<speak>Hello, XML!</speak>
</greeter>
</fizzbuzz>
YAML
# Variable
message: "Hello, YAML!"
# FizzBuzz function (represented as sequence)
fizzbuzz:
- 1
- 2
- Fizz
- 4
- Buzz
- Fizz
- 7
- 8
- Fizz
- Buzz
- 11
- Fizz
- 13
- 14
- FizzBuzz
# Class-like
greeter:
msg: "Hello, YAML!"
speak: "Hello, YAML!"
Zig
const std = @import("std");
// Variable
const message = "Hello, Zig!";
// FizzBuzz function
fn fizzbuzz(n: u32) []const u8 {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return std.fmt.allocPrint(std.heap.page_allocator, "{d}", .{n}) catch "err";
}
// Class-like (struct with method)
const Greeter = struct {
msg: []const u8,
fn speak(self: Greeter) void {
std.debug.print("{s}\n", .{self.msg});
}
};
pub fn main() void {
std.debug.print("{s}\n", .{message});
var i: u32 = 1;
while (i <= 15) : (i += 1) {
std.debug.print("{s}\n", .{fizzbuzz(i)});
}
const g = Greeter{ .msg = message };
g.speak();
}