Kodekraftt
/Blog
/Duck Number In Java Definition Rules Examples And Practice Problems
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Duck Number in Java: Definition, Rules, Examples, and Practice Problems

Learn what a Duck Number is in Java programming. Understand its rules, see examples, and learn how to check a Duck Number using Java. Includes 3 detailed practice challenges.

Duck Number in Java: Definition, Rules, Examples, and Practice Problems

A Duck Number is any positive integer that contains at least one zero, but the number must not begin with zero. Duck Numbers are commonly asked in basic number theory and programming interviews because they test a student's understanding of digit validation.

What Is a Duck Number?

A number is considered a Duck Number if:

  • It contains at least one zero.
  • It does not start with zero.

Examples:

  • 102, 705, 9081 → Duck Numbers
  • 123, 5678 → Not Duck Numbers (no zero)
  • 0123 → Not a Duck Number (starts with zero)

Why Is It Called a Duck Number?

The digit zero resembles a duck’s egg, which led to such numbers being informally named Duck Numbers.

Algorithm to Check a Duck Number

  1. Read the number as a string.
  2. Ensure the first digit is not zero.
  3. Check if zero appears in the remaining digits.

Java Program to Check Duck Number


import java.util.*;

public class DuckNumberCheck {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        String num = sc.next();

        if (num.charAt(0) == '0') {
            System.out.println("Not a Duck Number");
        } else {
            boolean hasZero = false;

            for (int i = 1; i < num.length(); i++) {
                if (num.charAt(i) == '0') {
                    hasZero = true;
                    break;
                }
            }

            if (hasZero) {
                System.out.println("Duck Number");
            } else {
                System.out.println("Not a Duck Number");
            }
        }
    }
}

  

Examples

Number Contains Zero? Starts With Zero? Duck Number?
204 Yes No Yes
9071 Yes No Yes
1203 Yes No Yes
123 No No No
0129 Yes Yes No

3 Practice Challenges

Challenge 1: Simple Check

Write a Java program to check whether the user-input number is a Duck Number without using string conversion.

Challenge 2: Count Duck Numbers

Given an array of integers, count how many of them are Duck Numbers.

Challenge 3: Generate Duck Numbers

Write a Java program to print all Duck Numbers between 1 and 50,000.

You might also like

Odious Number in Java: Definition, Rules, Examples, and Practice Problems

Learn what an Odious Number is in Java and number theory. Understand its definition, binary representation rules, examples, and how to check Odious Numbers using Java. Includes example table and 3 practice challenges.

Evil Number in Java: Definition, Examples, and Practice Problems

Learn what an Evil Number is in Java and number theory. Understand its definition, binary representation rules, examples, and how to check Evil Numbers using Java. Includes example table and 3 practice challenges.

Pronic Number in Java: Definition, Explanation, Examples, and Practice Problems

Learn what a Pronic Number is in Java with clear explanations, examples, and a complete Java program. Understand how to identify Pronic Numbers and practice with 3 coding challenges.

Strong Number in Java

Learn what a Strong Number is, how it works, and how to write an efficient Java program to check if a number is strong. Includes explanation, examples, algorithm, and clean Java implementation.

Spy Number in Java — Definition, Logic, Examples, and Program

Learn what a Spy Number is in Java with clear explanations and examples. This blog covers the definition, logic, step-by-step approach, and a clean Java program to check whether a number is a Spy Number. Includes practice challenges to improve your number theory skills.

Sunny Number in Java: Meaning, Explanation, Examples, and Practice Problems

Learn what a Sunny Number is in Java with definition, examples, logic, and a complete Java program. Understand how to check Sunny Numbers and practice with 3 coding challenges for students.