Home > Back-end >  Bash script to check if a number is a multiple of 3, 5 or both
Bash script to check if a number is a multiple of 3, 5 or both

Time:10-20

The variable D is supposed to contain a positive integer. My FizzBuzz program has to use arithmetic substitution and the ||, &&, () operators. If the number is a multiple of three, the program should output the word Fizz; if the number is a multiple of five, the word Buzz. If the number is a multiple of three and five, then the program should display the word FizzBuzz. I am a novice programmer, unsure where I have gone wrong.

"""

#!/bin/bash

D=5
if [ $D % 3 == 0 ] ;
then
echo "Fizz"
elif [[ $D % 5 == 0 ]]
then
echo "Buzz"
elif [[ $D % 3 == 0 && $D % 5 == 0 ]]
fi
echo "FizzBuzz"
done

"""

CodePudding user response:

To perform computations I recommend (( and )):

#!/bin/bash

d=5

if (( $d%3 == 0 && $d%5 == 0 )); then
  echo "FizzBuzz";
else
  if (( $d%3 == 0 )); then
    echo "Fizz"
  elif (( $d%5 == 0 )); then
    echo "Buzz"
  fi
fi
  • Related