PHP program to find if a number is present in a given sequence of numbers


To find if a number is present in a given sequence of numbers, the code is as follows −

Example

 Live Demo

<?php
function contains_in_sequence($val_1, $val_2, $val_3)
{
   if ($val_1 == $val_2)
      return true;
   if (($val_2 - $val_1) * $val_3 > 0 &&
      ($val_2 - $val_1) % $val_3 == 0)
      return true;
   return false;
}
$val_1 = 11; $val_2 = 99; $val_3 = 2;
print_r("Is the number present in the sequence? ");
if (contains_in_sequence($val_1, $val_2, $val_3))
   echo "Yes, it is present in the sequence";
else
   echo "No, it is not present in the sequence";
?>

Output

Is the number present in the sequence? Yes, it is present in the sequence

A function named ‘contains_in_sequence’ checks to see if two values are same, and if they are equal, the function returns true. If the difference between the two values multiplied by a third value is greater than 0 and the difference between the values divided by the third value gives a reminder as 0, the function returns true, otherwise, returns false. Values are assigned to the three variables and the function is called by passing these values to the function. Relevant output is displayed on the console.

Updated on: 02-Jul-2020

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements