Duck Numbers in Java



A duck number is a positive number that contains a zero in it, but the first digit of the number cannot be a zero. For example, 10305, 20050, and 603 are duck numbers, while 0349 and 2987 are not duck numbers, as 0349 starts with 0 and 2987 does not contain 0 in it.

The following are the approaches to find the duck number in Java ?

  • Using a while Loop
  • Using the contains() operator

Let's understand each approach in detail.

Finding Duck Number Using a While Loop

In this approach, we take a number as input and iterate through the digits starting from the end to find zeros using the modulus operator and division operator. If we find zero, it is a duck number.

Steps to implement

Follow these steps to determine if a number is a Duck Number using a while loop.

Step 1: Input the number as a string. We are taking the input as a string because if it is an integer, Java may convert numbers starting with zero, like 0523 into an octal number and may cause an error in computation.

Step 2: Check if the first digit is zero and return false if it starts with zero.

Step 3: Check if the number contains zero, starting from its end by calculating ?number % 10 ' and dividing the number by 10. If the number contains zero, then return True; else return False.

Implementation code

Below is the Java code demonstrating how to check for a Duck Number using a while loop.

import java.util.*;
public class ducknumber {
   public static boolean checkducknum(String number) {  
      if (number.charAt(0) == '0') {
         return false;
      }
      int num = Integer.parseInt(number);
      while (num != 0) {  
         if (num % 10 == 0)  
            return true;   
         num = num / 10;  
      }  
      return false; 
   }   
   public static void main(String args[]) {     
      String n = "1004"; 
      if (checkducknum(n))   
         System.out.println(n + " is a Duck number");   
      else  
         System.out.println(n + " is not a Duck number");   
   }
}

Output

If n is 1004, output is:
1004 is a duck number
If n is 0974, output is:
0974 is not a duck number

Time complexity

The time complexity of the code is O(n), where n is the number of digits in the number.

Finding Duck Number using 'contains()'

This code can be simplified further using the 'contains()' string operator. We can easily use this operator to find if the number contains a zero. This way the code looks concise and short.

Implementation Code

Below is the Java code demonstrating how to check for a Duck Number using the contains() method.

import java.util.*;
public class ducknumber {
   public static boolean checkducknum(String number) {  
      if (number.charAt(0) == '0') {
         return false;
      }
      return number.contains("0");
   }   
   public static void main(String args[]) {     
      String n = "01004"; 
      if (checkducknum(n))   
         System.out.println(n + " is a Duck number");   
      else  
         System.out.println(n + " is not a Duck number");   
   }  
}

Output

If n is 16078, output is:
16078 is a duck number
If n is 00765, output is:
00765 is not a duck number

Time complexity

The time complexity of the code is O(n)

Updated on: 2025-02-28T15:16:49+05:30

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements