首頁 >資料庫 >mysql教程 >創建一個程式來找出數字的階乘?

創建一個程式來找出數字的階乘?

王林
王林轉載
2023-08-29 15:05:08630瀏覽

創建一個程式來找出數字的階乘?

它可以在以下查詢的幫助下創建-

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刪除