Write a php program to find factors of a number using while loop & for loop
ADVERTISEMENTS
Write a php program to find factors of a number using while loop & for loop.
In this program, you'll learn how to find the factors of an input number by using the for loop & while loop.
Syntax of factors of a number:
1 2 3 4 5 6 10 ....
1. PHP program to find factors of a number using while loop:
<?php
// PHP program to find factors of a number using the while loop
$x = 90;
$i = 1;
echo "The factors of the " . $x . " are: ";
while ($i <= $x)
{
if ($x % $i == 0)
{
echo $i . " ";
}
++$i;
}
?>
Output:
The factors of the 90 are: 1 2 3 5 6 9 10 15 18 30 45 90
2. php program to find factors of a number using for loop:
<?php
// PHP program to find factors of a number using the for loop
$x = 90;
echo "The factors of the " . $x . " are: ";
for ($i = 1; $i <= $x; ++$i)
{
if ($x % $i == 0)
{
echo $i . " ";
}
}
?>
Output:
The factors of the 90 are: 1 2 3 5 6 9 10 15 18 30 45 90