Factorial program in php using while

Code



FACTORIAL OF A NUMBER


FACTORIAL OF A NUMBER





ENTER A NUMBER:




if(isset($_POST["t1"]))
{
$n=$_POST["t1"];
$fact=1;
echo "


";
echo "Factorial of $n is: ";

while($n>0)
{
$fact=$fact*$n;
$n--;
}
echo "$fact";
}
?>



Output

Factorial program in php using while


PHP program to find the factorial of a number

The factorial of a number n is defined by the product of all the digits from 1 to n (including 1 and n).

For example,

Note:

  • It is denoted by n! and is calculated only for positive integers.
  • Factorial of 0 is always 1.

The simplest way to find the factorial of a number is by using a loop.

There are two ways to find factorial in PHP:

  • Using loop
  • Using recursive method

Logic:

  • Take a number.
  • Take the descending positive integers.
  • Multiply them.

Factorial in PHP

Factorial of 4 using for loop is shown below.

Example:

Output:

Factorial program in php using while

Factorial using Form in PHP

Below program shows a form through which you can calculate factorial of any number.

Example:

Output:

Factorial program in php using while

Factorial using Recursion in PHP

Factorial of 6 using recursion method is shown.

Example:

Output:

Factorial program in php using while

The do while loop[edit | edit source]

The do while loop is similar in syntax and purpose to the while loop. The do/while loop construct moves the test that continues the loop to the end of the code block. The code is executed at least once, and then the condition is tested. For example:


$c = 6;
do {
  echo 'Hi';
} while ($c < 5);
?>

Even though $c is greater than 5, the script will echo "Hi" to the page one time.

PHP's do/while loop is not commonly used.

Example[edit | edit source]

This example will output the factorial of $number (product of all the numbers from 1 to it).

Factorial program in php using while
PHP Code:

$number = 5;
$factorial = 1;
do {
  $factorial *= $number;
  $number = $number - 1;
} while ($number > 0);
echo $factorial;

Factorial program in php using while
PHP Output:

Factorial program in php using while
HTML Render:

120

The continue statement[edit | edit source]

The continue statement causes the current iteration of the loop to end, and continues execution where the condition is checked - if this condition is true, it starts the loop again. For example, in the loop statement:


$number = 6;
for ($i = 3; $i >= -3; $i--) {
  if ($i == 0) {
    continue;
  }
  echo $number . " / " . $i . " = " . ($number/$i) . "
"
; } ?>

the statement is not executed when the condition i = 0 is true.

For More Information[edit | edit source]

  • PHP Manual: Control Structures: do-while

How do you calculate factorials on a while loop?

Factorial Program Using while Loop.
Declare a variable (int fact) and initialize it with 1..
Read a number whose factorial is to be found. ... .
Set the while loop to the condition (i <= num) where initial value of i = 1..
Inside the while loop, multiply the variable fact and variable i, and store the result in variable fact..

How do you calculate factorial in PHP?

There are two ways to find factorial in PHP: Using loop. Using recursive method..
$num = 4;.
$factorial = 1;.
for ($x=$num; $x>=1; $x--).
$factorial = $factorial * $x;.
echo "Factorial of $num is $factorial";.

What is factorial programming?

Factorial of a positive integer (number) is the sum of multiplication of all the integers smaller than that positive integer. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 which equals to 120. Factorial Program in C: All positive descending integers are added together to determine the factor of n.