How can I use conditional operators to improve my code?

1 次查看(过去 30 天)
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by