[TynesideLUG] Messing around with bash - fizzbuzz

Ian Bruntlett ian.bruntlett at gmail.com
Thu Jul 1 15:31:08 UTC 2021


Hi,

Been messing around with bash... anyone else been dabbling with things?

#!/usr/bin/env bash
# 2021-07-01 fizzbuzz Ian Bruntlett

# fizz_buzz number
# divisible by 3  - return FIZZ
# divisible by 5  - return BUZZ
# divisible by 15 - return FIZZBUZZ
function fizz_buzz()
{
  FIZZ_BUZZ=$1
  declare -i argument=$1
  let "remainder_by_3 = $argument % 3"
  let "remainder_by_5 = $argument % 5"
  let "remainder_by_15 = $argument % 15"

  if [ $remainder_by_15 -eq 0 ]; then
      FIZZ_BUZZ="FIZZ BUZZ"
  elif [ $remainder_by_3 -eq 0 ]; then
      FIZZ_BUZZ="FIZZ"
  elif [ $remainder_by_5 -eq 0 ]; then
      FIZZ_BUZZ="BUZZ"
  fi
}


while [ "$1" != "" ]; do
  fizz_buzz $1
  echo fizz_buzz $1 = $FIZZ_BUZZ
  shift
done

-- 
-- ACCU - Professionalism in programming - http://www.accu.org
-- My writing - https://sites.google.com/site/ianbruntlett/
-- Free Software page -
https://sites.google.com/site/ianbruntlett/home/free-software


More information about the Tyneside mailing list