Factors of a number in PHP

Factors are numbers which divide the number evenly.
Factors of 12 are 1, 2, 3, 4, 6, 12
Factors ( of a number ) are numbers which when divide the number they leave 0 as reminder.

Factors using loop

We will start from 1 and continue upto half of the number. There can't be any factor of the number from half value till the number. So there is no need to check ( upper limit of the loop ) beyond the half value of the number.
We may include the number itself as all numbers are divided by itself.
Math modulus returns us the reminder of a division, for all factors the reminder is always 0.
$n1=50; // change this number
for($i=1;$i<=($n1/2); $i++){
	if($n1%$i == 0 )
		echo " $i," ;
}
echo  $n1 ;
Output is here
1, 2, 5, 10, 25,50

Using sticky form

Learn more about sticky form here. User will enter a number and the factors of the number will be displayed on submit.
$n1=$_POST['n1'];
echo "<form method=POST action=''>
<input type=text name=n1 value=''>
<input type=submit value=Submit><br>";
for($i=1;$i<=($n1/2); $i++){
	if($n1%$i == 0 )
		echo " $i," ;
}
echo  $n1 ;

DEMO using sticky form

Enter a number and then Submit




Introduction to PHP Factorial of a number Sum of Digits of a number
Armstrong number Strong number
Basic Codes Check the string or number is Palindrome or not in PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer