Duck Number ?
If a number contains one or more zeroes but does not start with a zero, it is called a Duck Number. Examples of Duck numbers are 2405, 452094, 102 etc. Examples of numbers which are not Duck numbers are 1423, 5736, 0198 etc.
There are different methods to check for a duck number in java. This post will detail out a couple of those methods.

Method 1 : Using methods of String class
Read the number to be tested as a String from user input. Check if the String has a 0 using its contains method and check if the string begins with a 0 using startsWith method of String class.
Now apply appropriate logical operators to ensure that the string has one or more 0s and it does not begin with a 0.

import java.util.Scanner;

public class DuckNumberTester {

   public static void main(String[] args) {
      Scanner reader = new Scanner(System.in);
      System.out.println("Enter a number");
      // read the number as string
      String number = reader.nextLine();
      // close reader
      reader.close();
      //check whether number has 0 and it does not begins with 0
      if (number.contains("0") && !number.startsWith("0")) {
	 System.out.println("Number is a Duck number");
      } else {
	 System.out.println("Number is not a Duck number");
      }
   }
}

Output

Enter a number
3456
Number is not a Duck number

Enter a number
506
Number is a Duck number

Method 2 : Iterating the number and checking every digit

Iterate over the number read as String and compare each digit with 0 using charAt method of java.lang.String class.
If the digit is a zero, increment a counter variable. When iterating over the number and comparing the digit with 0, a check is performed whether the digit is the first digit.
If the digit is a 0 and is the first digit, a flag is set and the loop is terminated, since if the first digit is 0, the number cannot be a Duck number and there is no need of going forward.
At the end of loop, the condition that the number does not start with a 0 and has at least 1 zero is checked to determine whether the number is a Duck number or not.

import java.util.Scanner;

public class DuckNumberTester {

   public static void main(String[] args) {
      Scanner reader = new Scanner(System.in);
      System.out.println("Enter a number");
      // read the number as string
      String number = reader.nextLine();
      // close reader
      reader.close();
      int zeroCount = 0;
      boolean startsWithZero = false;
      // iterate over the number
      for (int digitIndex = 0; digitIndex < number.length(); digitIndex++) { 
         // get digit at current index
         char digit = number.charAt(digitIndex); 
         // check if the digit is a 0 
         if (digit == '0') { 
            // check if this is the first digit 
            if (digitIndex == 0) { 
               // set a flag and terminate the loop 
               startsWithZero = true;
               break;
            }
            // increment number of zeroes
            zeroCount++;
          }
       }
       // check if the number does not start with a zero and contains at least 1 zero
       if (!startsWithZero && zeroCount > 0) {
          System.out.println("Number is a Duck number");
       } else {
	  System.out.println("Number is not a Duck number");
       }
   }
}

Output

Enter a number
4508
Number is a Duck number

Enter a number
567
Number is not a Duck number

Hope the article was useful.

Leave a Reply