Try creating a FizzBuzz problem with a shell program

Introduction

I created it as a small story of Bash programming. I am writing an article at a rapid pace for some reason. By the way, this is the third installment.

What is FizzBuzz?

For FizzBuzz, please check here. Count up from 1

--Display "Fizz" when divisible by 3 --Display "Buzz" when divisible by 5 --Display "FizzBuzz" when it is divisible by both

And so on.

Source

#!/bin/bash

echo -n "Please input number => "
read num

for i in $(seq 1 ${num})
do
  if (( $i % 3 == 0 && $i % 5 == 0 ))
  then
    echo "FizzBuzz"
  else if (( $i % 3 == 0))
    then
      echo "Fizz"
    else if (( $i % 5 == 0 ))
      then
        echo "Buzz"
      else
      echo $i
      fi
    fi
  fi
  sleep 1
done

Execution result

$ ./fizzbuzz.sh
Please input number => 15
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

At the end

You can make it by applying a program that becomes aho when there are multiples of 3 and a character string of 3.

By creating this program

--Algorithm --Conditional expression --Conditional operator --read command

I understand.

Recommended Posts

Try creating a FizzBuzz problem with a shell program
Try programming with a shell!
4. Creating a structured program
Try embedding Python in a C ++ program with pybind11
Try creating a CRUD function
(For beginners) Try creating a simple web API with Django
Creating a wav file split program
Creating a decision tree with scikit-learn
Creating a simple app with flask
I tried to create Bulls and Cows with a shell program
[Python] A program that creates stairs with #
Try drawing a normal distribution with matplotlib
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Creating a login screen with Django allauth
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Try HTML scraping with a Python library
Creating a map of patent problem solutions with Guided LDA (first half)
Creating a shell script to write a diary
Try to solve a set problem of high school math with Python
Try drawing a map with python + cartopy 0.18.0
Try TensorFlow RNN with a basic model
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Try Tensorflow with a GPU instance on AWS
View today's weather forecast with a shell script
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Try to draw a life curve with python
Procedure for creating a LineBot made with Python
Try sending a message with Twilio's SMS service
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv
Flow of creating a virtual environment with Anaconda
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Try drawing a map with Python's folium package
A shell program that displays the Fibonacci sequence
FizzBuzz with Python3
Try making a simple website with responder and sqlite3
[Python] Try to read the cool answer to the FizzBuzz problem
Try creating a compressed file using Python and zlib
Try to make a command standby tool with python
Looking back on creating a web service with Django 1
Try to dynamically create a Checkbutton with Python's Tkinter
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
[GCP] Try a sample to authenticate users with Firebase
Problems when creating a csv-json conversion tool with python
Looking back on creating a web service with Django 2
From buying a computer to running a program with python
A shell program that becomes aho in multiples of 3
A sample to try Factorization Machines quickly with fastFM
Note that there was a version problem with networkx
Current directory when creating a new one with Jupyter
[Python] Create a Tkinter program distribution file with cx_Freeze
Notes on creating a virtual environment with Anaconda Navigator
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Using a Python program with fluentd's exec_filter Output Plugin
Using a python program with fluentd's exec Output Plugin