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.

  1. 3210 is a Duck number because it contains zero at the end of the number but not present at the beginning of it.
  2. 08237 is not a Duck number because it contains zero at the beginning of it.
  3. 7033 is a Duck number because it contains zero at the second position, not at the beginning.
  4. 030405 is not a Duck number because it also contains zero in starting of the number.
  5. 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.

  1. We first take a number.
  2. We then find the last digit of the number.
  3. If the last digit is zero, it is a Duck number.
  4. Otherwise, we remove that digit from the number.
  5. 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

Duck Number Java

Let's implement one more program to get all the Duck number in a given range

FindAllDuckNumber.java

Output

Duck Number Java




Latest Courses