Duck Number Program in Java with Examples

In this short tutorial I will be sharing what is Duck number, examples of Duck number, algorithm and program to check whether a given number is Duck number or not.

Read Also : Java Program to Check Neon Number 

What is Duck Number 

A number is called as Duck number if zeroes are present in it but there should be no zero present at the beginning of the number. 

Examples :

Number to check : 3100  
Number contains zero : Yes 
Zero at the beginning : No // 3100 is a Duck number
 
Number to check : 7430689  
Number contains zero : Yes 
Zero at the beginning : No // 7430689 is a Duck number

Below are examples of numbers which are NOT Duck numbers

Number to check : 07010  
Number contains zero : Yes 
Zero at the beginning : Yes // 07010 is NOT a Duck number
 
Number to check : 003189  
Number contains zero : Yes  
Zero at the beginning : Yes // 003189 is NOT a Duck number

List of Duck numbers from 1 to 100 are : 
10, 20, 30, 40, 50, 60, 70, 80, 90, 100

Java Program for Duck Number

Method 1 : Using for loop

import java.util.*;

public class JavaHungry {

    public static void main(String args[]) {
        System.out.println("Enter any number : ");
        Scanner scan = new Scanner(System.in);
        // Store the inputNumber in String
        String inputNumber = scan.nextLine();
        boolean result = checkDuckNumber(inputNumber);
        if(result == true)
            System.out.println(inputNumber + " is a Duck number");
        else
            System.out.println(inputNumber + " is NOT a Duck number");     
    }

    public static boolean checkDuckNumber(String inputNumber) {
        /* Calculating the length (number of digits) 
           of the inputNumber */
        int length = inputNumber.length();
        /* variable for counting number of zeros
           in inputNumber */        
        int countZero = 0;
        char ch;
        for(int i=0;i < length ;i++) {
        /* Using for loop, taking one digit at a time
           to check whether its '0' or not */    
            ch = inputNumber.charAt(i);
            if(ch == '0')
                countZero++;
        }
        /* Checking if first digit of the inputNumber
           starts with '0' */
        char beginWithZero = inputNumber.charAt(0);
        if(beginWithZero != '0' && countZero > 0)
            return true;
        else
            return false;
    }
}


Output :

Enter any number : 2783
2783 is NOT a Duck number

Method 2 : Using String functions only


import java.util.*;

public class JavaHungry {

    public static void main(String args[]) {
        System.out.println("Enter any number : ");
        Scanner scan = new Scanner(System.in);
        // Store the inputNumber in String
        String inputNumber = scan.nextLine();
        if(inputNumber.contains("0") && !inputNumber.startsWith("0"))
            System.out.println(inputNumber + " is a Duck number");
        else
            System.out.println(inputNumber + " is NOT a Duck number");     
    }
}

Output :

Enter any number : 20783
20783 is a Duck number

Algorithm for Duck Number

1. Calculate the length of the given number(inputNumber).

2. Initialize countZero variable value to 0. It will represent the number of zero digits in the inputNumber.

3. Using for loop
a. Taking one digit at a time of inputNumber, check whether its equal to '0'.  Increment countZero variable by 1 if digit is equal to '0'.

4. Store the value of first digit of inputNumber in variable beginWithZero.

5. Check whether the variable beginWithZero is not equal to '0' and countZero is greater than 0
     a. If both conditions are true then the inputNumber is a Duck number.
     b. Otherwise, the inputNumber is not a Duck number.

That's all for the day, please mention in comments if you know any other way to implement Duck number program in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry