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


41. Multiple of 13 or One More Than Multiple

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
// Define a function that checks if a number satisfies a certain condition related to modulo 13
function test($n)
{
    // Check if the remainder of $n divided by 13 is 0 or 1
    return $n % 13 == 0 || $n % 13 == 1;
}

// Test the function with different values
var_dump(test(13));
var_dump(test(14));
var_dump(test(27));
var_dump(test(41));
?>

Explanation:

  • Function Definition:
    • The test function checks whether a number $n meets a specific condition involving the modulo operation with 13.
  • Condition Checked:
    • The function checks if $n is either:
      • Divisible by 13 ($n % 13 == 0), or
      • Leaves a remainder of 1 when divided by 13 ($n % 13 == 1).
    • If either condition is met, it returns true; otherwise, it returns false.

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.

For more Practice: Solve these Related Problems:

  • Write a PHP script to check if a non-negative number is an exact multiple of 13 or exactly one more than a multiple of 13.
  • Write a PHP function to validate a number by computing its remainder when divided by 13 and comparing with 0 or 1.
  • Write a PHP program to use modulo arithmetic to determine if a number meets the criteria for multiples of 13 or one unit above.
  • Write a PHP script to simulate the condition check for multiples of 13 plus one using conditional statements.

Go to:


PREV : Check for 5, Sum or Difference Equals 5.
NEXT : Multiple of 3 or 7 but Not Both.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.