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

今日は、ループ処理の「階乗計算」の問題にチャレンジ!
console.log(N!); でいければいいのに…。


問題意識

  • 整数 N が与えられる。
  • N の階乗 N! を計算して出力。

入力例:

3

出力例:

6



✅OK例

const rl = require('readline').createInterface({input: process.stdin});

rl.once('line', (input) => {
  const N = Number(input);

  let factorial = 1; // 1でスタートが大事!
  for (let i = 1; i <= N; i++) {
    factorial *= i;
  }

  console.log(factorial);
  rl.close();
});



✅ 階乗

階乗とは:
N!=1×2×3×⋯×N
N!=1×2×3×⋯×N

例えば N=5 なら:
5!=1×2×3×4×5=120
5!=1×2×3×4×5=120



📝メモ

  • ループは 1 から N まで(N以下)でしっかり回すこと
  • 初期値の設定が大事




僕の失敗談(´;ω;`)と解決法🐈

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