1
0

More than 3 years have passed since last update.

【Ruby】多角形の内角の和を求めるメソッド

Posted at

【コード】

D = 3

def polygon(d)
  n = 180 * (d - 2)
  puts n
end

polygon(D)

【解説】

多角形の内角の和を求めるメソッドです。

変数Dに多角形の角数を代入します。

polygonメソッドを定義し、実引数(D)に角数を代入し、仮引数(d)に渡します。

メソッド内で内角の和を求める計算、180 * (角数(d) - 2)を実行し、nへ代入し、putsで出力します。

Dの値を変えることで、様々な多角形の内角の和を求めることができます。

また、D = gets.to_i とすることで、標準入力から値を取得することもできます。

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