Examples of Duck Numbers
Let us look at some examples of Duck numbers to understand the concept better:
- 2509 is a Duck number because it contains at least one zero and is not present at the beginning.
- 0931 is not a Duck number because it has a leading zero.
- 00777 is not a Duck number as it contains two zeros, but both present as leading zeros.
- 11005 is a Duck number as it includes two zeros, one at the 3rd position and another at the 4th position, but not at the start.
- 010902 is not a Duck number; it contains three zeros, but one of them is present at the start of the number.
Approach:
- Take the input number num as the string datatype.
- Find the length of the string num and initialize an integer variable zero = 0 to store the zeros in num.
- Start iterating over the characters in num, and if the character is equal to ‘0’, then increment the zeros variable, marking we encountered a zero in num.
- Now check if the first digit of this num is non-zero and the count of zeros in num is greater than 0.
- If the above condition is satisfied, it means num is a Duck Number. Otherwise, num is NOT a Duck Number.
Duck Number Java
In this article, we will look at a Duck Number in Java. We will look at its meaning and rules for a number to be Duck Number with the help of examples and write a Java program to demonstrate and verify it through our code.
Contact Us