- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to check if the total number of divisors of a number is even or odd
To check if the total number of divisors of a number is even or odd, the code is as follows −
Example
<?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
"; else echo "It is an odd number
"; } 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.
Advertisements