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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











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