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 1 year has passed since last update.

【PHP初級㉘】階乗

Last updated at Posted at 2022-07-09

[問題]  (参照:http://www.cc.kyoto-su.ac.jp/~mmina/bp1/hundredKnocksPrimary.html)
整数値を入力させ、その値の階乗を表示するプログラムを作成しなさい。ただし、0以下の値を入力した場合は1と表示する。

コード

$a = intval(fgets(STDIN));
if($a > 0){
    echo array_product(range(1, $a));
}else{
    echo '1';
}

↓「5」と入力

結果

120

↓「-2」と入力

1

☆array_product()
・・・配列の値の積を計算する関数。

2
0
2

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?