w3resource

PHP Exercises: Test if a given non-negative number is a multiple of 13 or it is one more than a multiple of 13

PHP Basic Algorithm: Exercise-41 with Solution

Write a PHP program to test if a given non-negative number is a multiple of 13 or it is one more than a multiple of 13.

Sample Solution:

PHP Code :

<?php
function test($n)
{
   return $n % 13 == 0 || $n % 13 == 1;
 }

var_dump(test(13));
var_dump(test(14));
var_dump(test(27));
var_dump(test(41));

Sample Output:

bool(true)
bool(true)
bool(true)
bool(false)

Flowchart:

Flowchart: Test if a given non-negative number is a multiple of 13 or it is one more than a multiple of 13.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program that accept two integers and return true if either one is 5 or their sum or difference is 5.
Next: Write a PHP program to check if a given non-negative given number is a multiple of 3 or 7, but not both.

What is the difficulty level of this exercise?



PHP: Tips of the Day

$_REQUEST: This SuperGlobal Variable is used to collect data submitted by a HTML Form

Example:

This code save in a php file and run in the browser.

<!DOCTYPE html> 
<html> 
<body> 
  
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
 NAME: <input type="text" name="user"> 
 <button type="submit">SUBMIT</button> 
</form> 
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $name = htmlspecialchars($_REQUEST['user']); 
    if(empty($name)){ 
        echo "Name is empty"; 
    } else { 
        echo $name; 
    } 
} 
?> 
</body> 
</html> 

Output:

Owen