PHP program to check if the total number of divisors of a number is even or odd

PHPServer Side ProgrammingProgramming

To check if the total number of divisors of a number is even or odd, the code is as follows −

Example

Live Demo

<?php
function divisor_count($my_val) {$my_count = 0;
for ($i = 1;$i <= sqrt($my_val) + 1;$i++)
{
if ($my_val %$i == 0)
$my_count += ($my_val / $i ==$i)? 1 : 2;
}
if (\$my_count % 2 == 0)
echo "It is an even number\n";
else
echo "It is an odd number\n";
}
divisor_count(100);
?>

Output

It is an odd number

A function named ‘divisor_count’ is defined that gives the number of divisors of a given number that is passed as a parameter to the function. Now, each of these divisors is checked to see if it can be completely divided by 2, if yes, it is an even divisor, and otherwise, it is an odd divisor. Relevant message is displayed on the console.

Published on 02-Jul-2020 06:57:54