Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Input : 5
    Output : 120
    
    Input : 10
    Output : 3628800

    Bàn luận In this method, we simply used the for loop to iterate over the sequence of numbers to get the factorial. 

    Chúng tôi đã biết làm thế nào để có được giai thừa của một số trong các ngôn ngữ khác. Hãy cùng xem cách thức này được thực hiện trong PHP bằng cách sử dụng cả cách đệ quy và không nhận được. Ví dụ:

    <?php

    Phương pháp 1: Cách lặp trong phương pháp này, chúng tôi chỉ cần sử dụng vòng lặp để lặp lại theo chuỗi số để có được giai thừa. & NBSP;

    PHP

    function

    3628800
    0
    3628800
    1
    3628800
    2

    3628800
    3
    3628800
    4
    3628800
    5

    3628800
    3
    3628800
    7
    3628800
    8
    3628800
    9
    3628800
    0
    3628800
    9

    3628800
    3
    3628800
    4
    3628800
    9
    3628800
    4
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    1
    3628800
    9
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    3

    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    7
    3628800
    4
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    3

    3628800
    1
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    2

    //example to calculate factorial of a number using function
    //defining the factorial function
    function Factorial_Function($number) {
    $input = $number;
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    return $fact;
    }
    //calling the factorial function
    $result = Factorial_Function(8);
    echo 'Factorial of the number 8 is '.$result;
    ?>
    0

    Output:

    3628800

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    3
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    4
    3628800
    1
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    6
    O(N) where N is the number of which factorial is being calculated

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    7
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    8
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    3
    O(1)

    Độ phức tạp về thời gian: O (n) trong đó n là số lượng mà giai thừa đang được tính toán In this method we are calling the same method to get the sequence of the factorial. 

    Chúng tôi đã biết làm thế nào để có được giai thừa của một số trong các ngôn ngữ khác. Hãy cùng xem cách thức này được thực hiện trong PHP bằng cách sử dụng cả cách đệ quy và không nhận được. Ví dụ:

    <?php

    Phương pháp 1: Cách lặp trong phương pháp này, chúng tôi chỉ cần sử dụng vòng lặp để lặp lại theo chuỗi số để có được giai thừa. & NBSP;

    PHP

    function

    3628800
    0
    3628800
    1
    3628800
    2

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    3628800
    3
    <?php
    //Example to demonstrate factorial of a number using recursion
    //function containing logic of factorial
    function Factorial_Function($input)
    {
    // if the input is less than or equal to 1 then return
    if($input <=1) {
    return 1;
    }
    // else do a recursive call and continue to find the factorial
    return $input * Factorial_Function($input-1);  //doing a recursive call
    }
    echo "Factorial of 9 is ".Factorial_Function(9);
    ?>
    7
    <?php
    //Example to demonstrate factorial of a number using recursion
    //function containing logic of factorial
    function Factorial_Function($input)
    {
    // if the input is less than or equal to 1 then return
    if($input <=1) {
    return 1;
    }
    // else do a recursive call and continue to find the factorial
    return $input * Factorial_Function($input-1);  //doing a recursive call
    }
    echo "Factorial of 9 is ".Factorial_Function(9);
    ?>
    8

    3628800
    3
    3628800
    4
    3628800
    5

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    5

    3628800
    3
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    7
    3628800
    4
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    3

    3628800
    1
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    2

    //example to calculate factorial of a number using function
    //defining the factorial function
    function Factorial_Function($number) {
    $input = $number;
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    return $fact;
    }
    //calling the factorial function
    $result = Factorial_Function(8);
    echo 'Factorial of the number 8 is '.$result;
    ?>
    0

    Output:

    3628800

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    3
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    4
    3628800
    1
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    6
    O(N) where N is the number of which factorial is being calculated

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    7
    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>
    8
    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>
    3
    O(N)


    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    GIỚI THIỆU VỀ THUỐC TRONG PHP

    Trước khi chúng ta bắt đầu học về giai thừa trong PHP, hãy để chúng tôi hiểu thuật ngữ. Lao động của một số là sản phẩm của tất cả các số bắt đầu từ 1 đến số chính. Trong khi tính toán sản phẩm của tất cả các số, số lượng được bao gồm.

    Lao động của một số chỉ được tính toán cho các số nguyên dương. Bộ phận của 0 luôn luôn là 1 và giai thừa của một số âm không tồn tại. Nó được ký hiệu là ‘! Trước đó là số. Ví dụ N! trong đó n là số

    So,

    Đơn vị của 5! có nghĩa là giai thừa 5

    Đơn vị của 7! có nghĩa là giai thừa của 7

    Ví dụ, giai thừa số 5 là:

    5! = 5*4*3*2*1 = 120

    Tương tự, giai thừa của số 7 là:

    7! = 7*6*5*4*3*2*1 = 5040

    và như thế..

    Bây giờ làm thế nào để chúng ta thực sự tìm thấy giai thừa, chúng ta có thể làm điều đó bằng cách sử dụng

    1. cho vòng lặp (không có đệ quy)
    2. với đệ quy

    Logic nhân viên

    Logic đằng sau việc nhận được giai thừa của số là theo cách sau.

    1. Nhận số mà có thể tính toán.
    2. Nhận tất cả các số bắt đầu từ 1 cho đến số đó.
    3. Nhận phép nhân của tất cả các số.

    Hãy nhớ đến giai thừa của 0! = 1.

    Làm thế nào để tìm thấy giai thừa trong PHP?

    Chúng tôi sẽ tìm hiểu thêm bằng cách sử dụng các phương pháp khác nhau để tính toán giai thừa của số đã cho bằng mã PHP. Giống như sử dụng đệ quy, đệ quy với đầu vào của người dùng, không có đệ quy, không có đệ quy với đầu vào của người dùng.

    Về đệ quy

    Giống như các ngôn ngữ khác PHP cũng hỗ trợ đệ quy. Đệ quy là gì? Khi một hàm gọi nó được gọi là đệ quy. Một hàm đệ quy tự gọi chính nó trong hàm.

    Ví dụ 1

    Trong giai thừa chương trình PHP sau đây của số 5 được tính toán. Đây là một chương trình đơn giản sử dụng cho Loop. Điều này cho vòng lặp được lặp lại trên chuỗi các số bắt đầu từ số cho đến 1.

    Code:

    <?php
    //example to calculate factorial of a number using simple for loop
    //declaring the input number as 5
    $input=5;
    //declaring the fact variable as 1
    $fact =1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    // multiply each number up to 5 by its previous consecutive number
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number 5 is '. $fact
    ?>

    Output::

    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    Ví dụ #2

    Trong chương trình dưới đây, chúng tôi đã sử dụng biểu mẫu HTML đơn giản với văn bản đầu vào và nút gửi. Hộp đầu vào được sử dụng để có được đầu vào của người dùng. Nút gửi được sử dụng để gửi dữ liệu biểu mẫu. Tiếp theo là mã PHP để lặp lại cho vòng lặp trong đó tất cả các logic có mặt, chúng tôi đã học trong chương trình trước đó. Vì vậy, bây giờ logic tương tự được sử dụng với một hình thức đầu vào.

    Nếu người dùng nhập số dương thông qua hộp đầu vào trong biểu mẫu, thì giai thừa của số đó sẽ được tính toán và kết quả được in.

    Code:

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    if($_POST['submit'] == "Submit") {
    $input = $_POST['number'];
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
    }
    ?>
    </body>
    </html>

    Đầu ra:

    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    Ví dụ #3

    Trong hai chương trình trên, chúng tôi đã kết thúc logic trong một hàm. Ở đây, chúng tôi đã đính kèm logic chính trong một hàm và sau đó được gọi là hàm đó để tính toán giai thừa của số đã cho trong PHP. Ở đây tên của hàm là Factorial_function tìm thấy giai thừa của số 8.

    Code:

    //example to calculate factorial of a number using function
    //defining the factorial function
    function Factorial_Function($number) {
    $input = $number;
    $fact=1;
    //iterating using for loop
    for($i=$input; $i>=1;$i--) {
    $fact = $fact * $i;
    }
    return $fact;
    }
    //calling the factorial function
    $result = Factorial_Function(8);
    echo 'Factorial of the number 8 is '.$result;
    ?>

    Đầu ra: :

    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    Ví dụ #3

    Trong hai chương trình trên, chúng tôi đã kết thúc logic trong một hàm. Ở đây, chúng tôi đã đính kèm logic chính trong một hàm và sau đó được gọi là hàm đó để tính toán giai thừa của số đã cho trong PHP. Ở đây tên của hàm là Factorial_function tìm thấy giai thừa của số 8.

    Ví dụ #4

    Code:

    <?php
    //Example to demonstrate factorial of a number using recursion
    //function containing logic of factorial
    function Factorial_Function($input)
    {
    // if the input is less than or equal to 1 then return
    if($input <=1) {
    return 1;
    }
    // else do a recursive call and continue to find the factorial
    return $input * Factorial_Function($input-1);  //doing a recursive call
    }
    echo "Factorial of 9 is ".Factorial_Function(9);
    ?>

    Đầu ra::

    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    Ví dụ #3

    Trong hai chương trình trên, chúng tôi đã kết thúc logic trong một hàm. Ở đây, chúng tôi đã đính kèm logic chính trong một hàm và sau đó được gọi là hàm đó để tính toán giai thừa của số đã cho trong PHP. Ở đây tên của hàm là Factorial_function tìm thấy giai thừa của số 8.

    Code:

    <html>
    <head>
    <title> Factorial Program</title>
    </head>
    <body>
    <form method="POST">
    <label>Enter a number</label>
    <input type="text" name="number" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    // example to demonstrate factorial of a number using form
    function Factorial_Function($input)
    {
    // if the input is less than or equal to 1 then return
    if($input <=1) {
    return 1;
    }
    // else do a recursive call and continue to find the factorial
    return $input * Factorial_Function($input-1); //doing a recursive call
    }
    if(!empty($_POST['number'])){
    $input = $_POST['number'];
    // Print output of the program
    echo '<br>'. 'The factorial of the number '.$input.' is ' . Factorial_Function($input);
    }
    ?>
    </body>
    </html>

    Output:

    Hướng dẫn how do you find the factorial of a number in php? - làm cách nào để tìm giai thừa của một số trong php?

    Ví dụ #4

    Chúng tôi biết rằng đệ quy đang gọi một hàm trong một hàm. Trong ví dụ sau, chúng tôi sẽ sử dụng đệ quy và tìm giai thừa của số bằng mã PHP. Logic chính được bọc trong một tên hàm factorial_function. Trong hàm này nếu đầu vào lớn hơn, thì cùng một hàm được gọi lại và nếu đầu vào nhỏ hơn hoặc bằng 1 thì một hàm được trả về.

    Sử dụng đệ quy

    Ví dụ #5

    1. Bây giờ chúng tôi đã học được về đệ quy. Trong chương trình sau, chúng tôi đã sử dụng đệ quy, đệ quy được áp dụng cho số là đầu vào từ người dùng trong ví dụ này.
    2. Sự kết luận
    3. Bài viết này đã đề cập đến tất cả các giải thích và ví dụ để tìm ra giai đoạn của một số sử dụng PHP. Các ví dụ được giải thích bằng cách sử dụng các cách đệ quy và không tái cấu trúc, cùng với lời giải thích đệ quy trong bối cảnh với chương trình. Hy vọng bài viết này đã được tìm thấy thông tin để học và nắm bắt tốt.
    4. Bài viết đề xuất

    Công thức để tìm thấy giai thừa của một số là gì?

    Trong các thuật ngữ toán học hơn, giai thừa của một số (n!) Bằng n (n-1). Ví dụ: nếu bạn muốn tính toán giai thừa cho bốn, bạn sẽ viết: 4! = 4 x 3 x 2 x 1 = 24.n(n-1). For example, if you want to calculate the factorial for four, you would write: 4! = 4 x 3 x 2 x 1 = 24.

    Làm thế nào để bạn tìm thấy giai thừa của một số bằng một mảng?

    Các công thức đệ quy để tính toán giai thừa của một số là: thực tế (n) = n*thực tế (n-1). Do đó, chúng tôi sẽ xây dựng một mảng theo cách từ dưới lên bằng cách sử dụng đệ quy ở trên. Khi chúng tôi đã lưu trữ các giá trị trong mảng thì chúng tôi có thể trả lời các truy vấn trong thời gian O (1).fact(N) = N*fact(N-1). Hence, we will build an array in a bottom-up manner using the above recursion. Once we have stored the values in the array then we can answer the queries in O(1) time.

    Đệ quy viết tập lệnh PHP để tìm giai thừa bằng cách sử dụng đệ quy là gì?

    Chương trình PHP để sử dụng Factorial bằng cách sử dụng Php /*Hàm đệ quy* / Factorial ($ n) {if ($ nfunction factorial($n) { if($n <= 1) { return 1; } else{ return $n * factorial($n - 1); } } /* Define a variable and assign a value to it to find the factorial. */ $n = 5; echo "Factorial of $n is " . factorial($n); /*Call the Function */ ?>

    Làm thế nào để bạn tìm thấy giai thừa trong ngôn ngữ lắp ráp?

    Thuật toán -..
    Nhập số có giai thừa sẽ được tìm và lưu trữ số đó trong thanh ghi CX (điều kiện cho hướng dẫn vòng lặp).
    Chèn 0001 vào AX (điều kiện cho lệnh MUL) và 0000 trong DX ..
    Nhân CX với AX cho đến khi CX trở thành 0 (0) bằng cách sử dụng hướng dẫn vòng lặp ..
    Sao chép nội dung của AX vào vị trí bộ nhớ 0600 ..