[Solved] Division - PHP Task with examples

  

3
Topic starter

You will be given an integer and you have to print on the console whether that number is divisible by the following numbers: 2, 3, 6, 7, 10. You should always take the bigger division. If the number is divisible by both 2 and 3 it is also divisible by 6 and you should print only the division by 6. If a number is divisible by 2 it is sometimes also divisible by 10 and you should print the division by 10. If the number is not divisible by any of the given numbers print “Not divisible”. Otherwise print "The number is divisible by {number}".

Examples:

division task php

1 Answer
2

We start from the biggest number - logic...

Here is the solution:

<?php
 
$number = intval(readline());
 
if ($number % 10 == 0) {
    echo 'The number is divisible by 10';
} else if ($number % 7 == 0) {
    echo 'The number is divisible by 7';
} else if ($number % 6 == 0) {
    echo 'The number is divisible by 6';
} else if ($number % 3 == 0) {
    echo 'The number is divisible by 3';
} else if ($number % 2 == 0) {
    echo 'The number is divisible by 2';
} else {
    echo 'Not divisible';
}
Share: