PHP program to check if a given number is present in an infinite series or not

PHPServer Side ProgrammingProgramming

To check if a given number is present in an infinite series or not, the PHP code is as follows −

Example

 Live Demo

<?php
   function contains_val($m, $n, $o){
      if ($m == $n)
         return true;
      if (($n - $m) * $o > 0 && ($n - $m) % $o == 0)
         return true;
         return false;
   }
   $m = 3; $n = 5; $o = 9;
   if (contains_val($m, $n, $o))
      echo "The number is present in the infinite series";
   else
      echo "The number is not present in the infinite series";
?>

Output

The number is not present in the infinite series

Above, three variables are defined, and the function is called by passing these three values −

$m = 3; $n = 5; $o = 9;

A function named ‘contains_val’ is defined, that takes these three variables. The first two variables are compared, and if they are equal, ‘true’ is returned. Otherwise, if the different between the first two variables with the product of the third number is greater than 0 and the different between the first two variables modulus the third number is 0, ‘true’ is returned. Otherwise, the function returns false −

function contains_val($m, $n, $o){
   if ($m == $n)
      return true;
   if (($n - $m) * $o > 0 && ($n - $m) % $o == 0)
      return true;
   return false;
}
raja
Published on 17-Aug-2020 15:46:53
Advertisements