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

Lớp hướng dẫn (hướng dẫnclass.com) là một cửa hàng dừng để tìm hiểu trực tuyến về các công nghệ web khác nhau, chuẩn bị cho một cuộc phỏng vấn và tăng cường các kỹ năng kỹ thuật của bạn.

Chúng tôi cung cấp các hướng dẫn trực tuyến miễn phí về các công nghệ web mới nhất. Những hướng dẫn này có cấu trúc tốt và dễ sử dụng cho người mới bắt đầu. Với mỗi hướng dẫn, bạn có thể tìm thấy một danh sách các bài tập, bài tập, mã, bài viết và câu hỏi phỏng vấn liên quan.

Trang web này cung cấp các hướng dẫn về PHP, HTML, CSS, SEO, C, C ++, JavaScript, WordPress và tiếp thị kỹ thuật số cho người mới bắt đầu. Bắt đầu học ngay bây giờ.

Phương pháp 2: Sử dụng đệ quy trong phương pháp này, chúng tôi đang gọi cùng một phương thức để có được chuỗi của giai thừa. & NBSP;

PHP cho vòng lặp: Tập thể dục-5 với giải pháp

Viết một chương trình để tính toán và in giai thừa của một số bằng cách sử dụng một vòng lặp. Bộ phận của một số là sản phẩm của tất cả các số nguyên lên đến và bao gồm số đó, vì vậy, giai thừa của 4 là 4*3*2*1 = 24.

Trình bày bằng hình ảnh:

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

Giải pháp mẫu:

Mã PHP:

<?php
$n = 6;
$x = 1;
for($i=1;$i<=$n-1;$i++)
{
 $x*=($i+1); 
}
echo "The factorial of  $n = $x"."\n";
?>

Đầu ra mẫu:

The factorial of  6 = 720

Sơ đồ:

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

Trình chỉnh sửa mã PHP:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đây: Tạo một tập lệnh để xây dựng mẫu cụ thể, sử dụng một bản lồng cho Loop.next: Viết một chương trình sẽ cung cấp cho bạn tất cả các kết hợp tiềm năng của một tổ hợp thập phân hai chữ số, được in theo định dạng dấu phẩy. Create a script to construct the specific pattern, using a nested for loop.
Next: Write a program which will give you all of the potential combinations of a two-digit decimal combination, printed in a comma delimited format.

PHP: Lời khuyên trong ngày

PHP: Đưa một mảng này vào một mảng khác (không phải mảng_push hoặc +)

Array_merge là cách thanh lịch:

$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');

Làm điều gì đó như:

$merge = $a + $b;
// $merge now equals array('a','b')

Sẽ không hoạt động, bởi vì toán tử + không thực sự hợp nhất chúng. Nếu họ $ A có các phím tương tự như $ B, nó sẽ không làm gì cả.

Tham khảo: https://bit.ly/357w9ga

Chương trình PHP để tìm giai thừa của một số sử dụng cho Loop. Trong bài viết này, bạn sẽ học cách tạo chương trình PHP để tìm giai thừa của một số sử dụng cho Loop.

Công thức số giai thừa

Factorial của x & nbsp; (n!) = 1 * 2 * 3 * 4 .... n

Mã nguồn

<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>

Chạy chương trình

Đầu ra

Factorial of 9 = 362880

Phương pháp 2: Sử dụng đệ quy trong phương pháp này, chúng tôi đang gọi cùng một phương thức để có được chuỗi của giai thừa. & NBSP;

The factorial of  6 = 720
4
Input : 5
Output : 120

Input : 10
Output : 3628800
8
The factorial of  6 = 720
9
The factorial of  6 = 720
2
3628800
1

3628800
2
$merge = $a + $b;
// $merge now equals array('a','b')
8
3628800
4

Input : 5
Output : 120

Input : 10
Output : 3628800

The factorial of  6 = 720
4
3628800
8
3628800
9
In this method, we simply used the for loop to iterate over the sequence of numbers to get the factorial. 

PHP

<?php

The factorial of  6 = 720
0
The factorial of  6 = 720
1
The factorial of  6 = 720
2

The factorial of  6 = 720
4
The factorial of  6 = 720
5
The factorial of  6 = 720
6

The factorial of  6 = 720
4
The factorial of  6 = 720
8
The factorial of  6 = 720
9
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
1
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
3
The factorial of  6 = 720
2

The factorial of  6 = 720
4
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
0
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
2
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$merge = $a + $b;
// $merge now equals array('a','b')
4

The factorial of  6 = 720
4
$merge = $a + $b;
// $merge now equals array('a','b')
6

The factorial of  6 = 720
4
$merge = $a + $b;
// $merge now equals array('a','b')
8
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
4

$merge = $a + $b;
// $merge now equals array('a','b')
6

The factorial of  6 = 720
2
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
3

<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
4
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
5__12

<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
8
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
9
$merge = $a + $b;
// $merge now equals array('a','b')
4

Input : 5
Output : 120

Input : 10
Output : 3628800
1

Output:

3628800

Độ 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ánO(N) where N is the number of which factorial is being calculated

Không gian phụ trợ: O (1)O(1)

Phương pháp 2: Sử dụng đệ quy trong phương pháp này, chúng tôi đang gọi cùng một phương thức để có được chuỗi của giai thừa. & NBSP; In this method we are calling the same method to get the sequence of the factorial. 

PHP

<?php

The factorial of  6 = 720
0
The factorial of  6 = 720
1
The factorial of  6 = 720
2

The factorial of  6 = 720
4
The factorial of  6 = 720
5
The factorial of  6 = 720
6

The factorial of  6 = 720
4
The factorial of  6 = 720
8
The factorial of  6 = 720
9
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
1
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
3
The factorial of  6 = 720
2

The factorial of  6 = 720
4
$merge = $a + $b;
// $merge now equals array('a','b')
6

The factorial of  6 = 720
4
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
0
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
2
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');
0
$merge = $a + $b;
// $merge now equals array('a','b')
4

The factorial of  6 = 720
4
$merge = $a + $b;
// $merge now equals array('a','b')
8
The factorial of  6 = 720
5
$merge = $a + $b;
// $merge now equals array('a','b')
4

The factorial of  6 = 720
4
$merge = $a + $b;
// $merge now equals array('a','b')
6

$merge = $a + $b;
// $merge now equals array('a','b')
6

The factorial of  6 = 720
2
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
3

<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
4
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
5__12

<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
8
<?php
// PHP Program to Find Factorial of a Number using For loop

$x = 9;
$f = 1;
// $x - To store the input number
// $f - To store the factorial value

if ($x > 0) {
    for ($i = 1; $i <= $x; $i++) {
        $f *= $i;
    }
    echo "Factorial of " . $x . " = " . $f . "\n";
} else {
    echo "Sorry, The input number should be positive number& it's greater than 0!\n";
}

?>
9
$merge = $a + $b;
// $merge now equals array('a','b')
4

Input : 5
Output : 120

Input : 10
Output : 3628800
1

Output:

3628800

Độ 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 O(N) where N is the number of which factorial is being calculated

Không gian phụ trợ: O (1) O(N)