How can I use conditional operators to improve my code?

1 回表示 (過去 30 日間)
zachary owen
zachary owen 2019 年 10 月 13 日
コメント済み: Daniel M 2019 年 10 月 13 日
Hello, i'm new to Matlab and I was wondering if someone could help me find the fault in my code. I've written the script below, and my code is posted below that. My issue is that my integer values in the command window display horizontally rather than vertically even after i've transposed it. I'm not concerned with the sum of the 4 factorials portion of the script just yet. Thanks!
This is what i'm trying to accomplish:
For N<12, generate a script that displays n!, n = 1,2,.....,N and the sum of these factorials as shown for N = 4.
Enter an integer < 12: 4
For n = 1, 1! = 1
For n = 2, 2! = 2
For n = 3, 3! = 6
For n = 4, 4! = 24
The sum of these 4 factorials = 33
Where the value of 4 was enetered by the user.

採用された回答

Daniel M
Daniel M 2019 年 10 月 13 日
N = [1:n];
s = [N;N;factorial(N)];
fprintf('For N = %d, %d! = %d\n',s);
  2 件のコメント
Stephen23
Stephen23 2019 年 10 月 13 日
The square brackets on the first line are superfluous:
Daniel M
Daniel M 2019 年 10 月 13 日
Thanks, bad habit.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by