Write a program find Factorial of a number

Here we are writing a program of print the factorial of a number, factorial means 1 to n multiply them where n is defined by the product of all the digits from 1 to n.

Important Points About Factorial:

1. Factorial is denoted by n!.
2. Factorial is calculated only for positive integers(9) not for nagative integers(-2).
3. Do you know Factorial of 0 is always 1.
4. The best and simle way to calculate the factorial of a number is by using a loop or recursive method.

Here we are calculating factorial of number using two method one is using while and second using for loop.

PHP Program to find factorial of number using while loop:

<?php
$num = 5;
$factorial = 1;
while($num>=1)
{
 $factorial = $factorial*$num;
 $num--;
}
echo "Factorial of $num is $factorial"; 
?>

PHP Program to find factorial of number using for loop:

<?php 
$num = 5; 
$factorial = 1;
for ($i=$num; $i>=1; $i--)
{
 $factorial = $factorial * $i;
}
echo "Factorial of $num is $factorial";
?>

PHP program to Print factorial of a number using HTML Form

<form method="post">
<input type="text" name="number" placeholder="Enter number" />
<input type="submit" value="Submit" />
</form>

<?php
if(isset($_POST['number']))
{
$num = $_POST['number'];
$factorial = 1;
for ($i=$num; $i>=1; $i--)
{
$factorial = $factorial * $i;
}
echo "Factorial of $num is $factorial";
}
?>

Keywords:
PHP program to Print factorial of a number, find factorial of a number, calculate factorial of a number, Program for factorial of a number, Factorial Calculator n!, factorial in php, factorial of a number in php, factorial of number using for loop, factorial of number using while loop, factorial calculator, factorial list

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.