71
Duck Number Java
Duck number is another special positive non-zero number that contains zero in it. The digit zero should not be presented at the starting of the number. Zero can be present at any of the positions except the beginning of the number.
Let’s understand some examples of Duck numbers.
- 3210 is a Duck number because it contains zero at the end of the number but not present at the beginning of it.
- 08237 is not a Duck number because it contains zero at the beginning of it.
- 7033 is a Duck number because it contains zero at the second position, not at the beginning.
- 030405 is not a Duck number because it also contains zero in starting of the number.
- 00153 is also not a Duck number because it contains leading zeros.
These are the following steps that we use to check whether the given number is a Duck number or not.
- We first take a number.
- We then find the last digit of the number.
- If the last digit is zero, it is a Duck number.
- Otherwise, we remove that digit from the number.
- Perform steps 2, 3, and 4 until the number becomes zero.
Let’s implement the code to check a valid Duck number.
DuckNumber.java
Output
Let’s implement one more program to get all the Duck number in a given range
FindAllDuckNumber.java
Output
Next TopicEvil Number Java