对任意给定正整数N,输出N的阶乘,并计算5的阶乘。分别编写命令式M文件和函数M文件
时间: 2024-03-01 15:54:23 浏览: 55
命令式M文件实现:
```matlab
N = input("请输入一个正整数N:");
factorial_N = 1;
for i = 1:N
factorial_N = factorial_N * i;
end
disp("N的阶乘为:" + factorial_N);
factorial_5 = 1;
for i = 1:5
factorial_5 = factorial_5 * i;
end
disp("5的阶乘为:" + factorial_5);
```
函数M文件实现:
```matlab
function [factorial_N, factorial_5] = compute_factorial(N)
% 计算N的阶乘和5的阶乘
factorial_N = 1;
for i = 1:N
factorial_N = factorial_N * i;
end
factorial_5 = 1;
for i = 1:5
factorial_5 = factorial_5 * i;
end
end
```
使用示例:
```matlab
>> [factorial_N, factorial_5] = compute_factorial(6)
factorial_N =
720
factorial_5 =
120
```
阅读全文