How can I use conditional operators to improve my code?

1 view (last 30 days)
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.

Accepted Answer

Daniel M
Daniel M on 13 Oct 2019
N = [1:n];
s = [N;N;factorial(N)];
fprintf('For N = %d, %d! = %d\n',s);

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!