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.

Similar Reads

What is Duck Number?

A Duck Number is a positive non-zero number containing at least one zero in its numeric representation. The zero should be present as a leading zero, i.e. the digit zero can be present at any position except at the start of the number....

Examples of Duck Numbers

Let us look at some examples of Duck numbers to understand the concept better:...

Program to check whether a given number is a Duck Number or not

Below is the Java program to check whether the given number is a Duck Number or not....

Contact Us