Create a code that generate a list of numbers based on the entered number

Hello, I am looking for a php code to do the following:
i have a number lets say: 00711
I want it to generate for me 10 other numbers from this number. and the results must be as follows:
00712
00710
00721
00701
00811
00611
01711
09711
10711
90711
the main number is 00711, the first result is by increment the fifth digit of the main number by one (00712), and the second result is by decrements the fifth digit of the main number by one (00710).
The third result is by increment the fourth digit of the main number by one (00721), and the forth result is by decrements the fourth digit of the main number by one (00701).
and so on.
the increment is regular, 1 increment to 2 , 2 after increment become 3, but when we reach number 9, it increment to 0 , not 10, and also about zero, the decrements for it is 9.
so the number will be like this (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,…) so after 9 it will be 0 and before 0 it will be 9.
if you notice before the last result (10711), we take the main number (00711) , we take the first digit (which is zero) and we decrements it to 9, so the result will be 90711.
i need the code to be done in PHP in order to add it to my website.
Thanks in advance.

Why would you need to do something like this?

Homework :slight_smile:

1 Like

Hi @kapoka and a warm welcome to the forum.

Can you show what you have tried, also the results and why it is not working?

I would be tempted to:

  1. Validate the string and make sure it conforms to the given criteria.
  2. Create a common function with the following:
    a. Original number
    b. String position to change
    c. Boolean for increment of decrement
    d. Return new string result
  3. For/next loop with id=1…3
  4. Call common function with relevant parameters
  5. Repeat step #4 until finished

I think step #5 could be further optimised.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.