
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find If a Number is Present in a Given Sequence of Numbers in PHP
To find if a number is present in a given sequence of numbers, the code is as follows −
Example
<?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.
Advertisements