11Example programs
FizzBuzz
A classic counting game, built from functions, booleans and if/else.
FizzBuzz is a counting game. Count up from 1. Call out Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number itself for all the rest.
စံကား
Expected output
၁
၂
Fizz
၄
Buzz
Fizz
၇
၈
Fizz
Buzz
၁၁
Fizz
၁၃
၁၄
FizzBuzz
How it works
ပြတ်သလား("does it divide?") returns a boolean: whetherန % ဆခွဲis zero. Its name is a question, which keeps the code readable.FizzBuzzstores the two answers in variablesသုံးပြတ်("3 divides it") andငါးပြတ်("5 divides it"), then picks one of four outputs with an if / else-if / else chain.- The test order matters. "Both" must come first. 3 divides 15, so if the Fizz test came first, 15 would print Fizz.
- The main program is two lines: a constant for where to
stop, and a counted loop that calls
FizzBuzzonce per number.
Hands on
- Change
အဆုံးto 100. - Add a rule: print Bazz for multiples of 7.