Home > Database > Mysql Tutorial > Create a program to find the factorial of a number?

Create a program to find the factorial of a number?

王林
Release: 2023-08-29 15:05:08
forward
634 people have browsed it

Create a program to find the factorial of a number?

It can be created with the help of the following query -

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)
Copy after login

Now when this procedure is called by passing the value we want to get the factorial as parameter When-

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)
Copy after login

The above is the detailed content of Create a program to find the factorial of a number?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template