PHP Exercises: Check if a given positive number is a multiple of 3 or a multiple of 7
PHP Basic Algorithm: Exercise-10 with Solution
Write a PHP program to check if a given positive number is a multiple of 3 or a multiple of 7.
Sample Solution:
PHP Code :
<?php
// Define a function named "test" that takes a parameter $n
function test($n)
{
// Use the modulo operator to check if $n is divisible by 3 or 7
return $n % 3 == 0 || $n % 7 == 0;
}
// Call the test function with argument 3 and var_dump the result
var_dump(test(3));
// Call the test function with argument 14 and var_dump the result
var_dump(test(14));
// Call the test function with argument 12 and var_dump the result
var_dump(test(12));
// Call the test function with argument 37 and var_dump the result
var_dump(test(37));
?>
Sample Output:
bool(true) bool(true) bool(true) bool(false)
Visual Presentation:
Flowchart:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP program to create a new string with the last char added at the front and back of a given string of length 1 or more.
Next: Write a PHP program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics