首页 >数据库 >mysql教程 >创建一个程序来找出数字的阶乘?

创建一个程序来找出数字的阶乘?

王林
王林转载
2023-08-29 15:05:08634浏览

创建一个程序来找出数字的阶乘?

它可以在以下查询的帮助下创建 -

mysql> Delimiter //
mysql> CREATE PROCEDURE fact(IN x INT)
    -> BEGIN
    -> DECLARE result INT;
    -> DECLARE i INT;
    -> SET result = 1;
    -> SET i = 1;
    -> WHILE i <= x DO
    -> SET result = result * i;
    -> SET i = i + 1;
    -> END WHILE;
    -> SELECT x AS Number, result as Factorial;
    -> END//
Query OK, 0 rows affected (0.17 sec)

现在,当通过传递我们想要获取阶乘作为参数的值来调用此过程时 -

mysql> Delimiter ;
mysql> CALL Fact(5);
+--------+-----------+
| Number | Factorial |
+--------+-----------+
|      5 |       120 |
+--------+-----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL Fact(6);
+--------+-----------+
| Number | Factorial |
+--------+-----------+
|      6 |       720 |
+--------+-----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

以上是创建一个程序来找出数字的阶乘?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除