LoginSignup
0
0

More than 1 year has passed since last update.

差分で出力を変える

Last updated at Posted at 2021-06-04

問題

正の整数を入力します。その整数が、
10の倍数(10,20,30...)からの差が2以内であるときはTrue
それ以外はFalse
と出力するメソッドを作りましょう。

出力例:
near_ten(12)→True
near_ten(17)→False
near_ten(19)→True

 ○○.rb
def near_ten(num)
  quotient = num % 10
  if quotient <= 2 || quotient >= 8
    puts "True"
  else
    puts "False"
  end
end

解説

「10の倍数からの差」を考えるためには、一の位の値に着目します。すなわち、
一の位が「0,1,2,8,9」のどれかであれば「10の倍数からの差が2以内」と判断することができます。
したがって、変数quotientに一の位の値を代入し、quotient <= 2 || quotient >= 8で「0,1,2,8,9」
のどれかに当てはまるかどうかを確認しています。

終わり

今日はオリジナルアプリの作成が捗らず、rubyの復習をかねて練習問題を行いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0