2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rubyで値が〇の倍数であることを判別する

Last updated at Posted at 2021-01-10

どうもこんにちは。
Rubyを始めてみたのですが任意の値が倍数かどうか確かめる方法が調べてもなかなか出てこなかったのでメモ。

判別には

AをBで割った余りを出してくれる % を利用します。
指定した倍数であれば0が出力されるはずです。

コード

sample.rb
a = 10
b = 5
if a % b == 0
 puts "#{a}は5の倍数です。"
 else
 puts "#{a}は5の倍数でありません。"
end
2
0
1

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?