Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Duck Number

Updated 21 Oct 2026

A Duck number is a number in which the digit zero is present, but not as its first digit. This program is written in the main method alone: it reads the number as an int, takes out its digits one by one from the right with % 10 and / 10, and looks for a zero.

Definition

A Duck number is a number that contains the digit 0, where the first digit is not 0. A number has no leading zero, so for a number written normally this means: the digit 0 is present in it.

  • $3210$ is a Duck number: it has a zero and starts with 3.
  • $7056$ is a Duck number: the zero is in the second place.
  • $100$ and $1203$ are Duck numbers.
  • $123$ is not a Duck number: there is no zero.
  • The number $0$ itself is taken as a Duck number, because the digit zero is present in it.
  • A negative number is not accepted: the program prints "Invalid input".

Because the number is stored as an int, a leading zero is never kept, so the only thing to check is whether some digit is 0.

Algorithm

  1. Read the input. If it is not an integer, or it is negative, print Invalid input and stop. (sc.hasNextInt() tells whether the input is an integer.)
  2. Set duck = false. If n is 0, its only digit is the digit zero, so set duck = true.
  3. Copy n into t so that n is not destroyed. While t > 0:
    • find the last digit d = t % 10;
    • if d == 0, set duck = true;
    • drop the last digit with t = t / 10.
  4. An int never has a leading zero, so any zero found is not the first digit. If duck is true print that n is a Duck number, otherwise print that it is not.

Program

The while loop in main takes out the digits one at a time and sets the boolean duck when it meets a zero.

import java.util.*;
/* Program DuckNumber checks whether a number is a Duck number:
   the digit 0 is present in it, but not as its first digit. */
public class DuckNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);   // reads input from the keyboard
        System.out.print("Enter a number: ");
        if (sc.hasNextInt())
        {
            int n = sc.nextInt();               // the number to be checked
            if (n < 0)
            {
                System.out.println("Invalid input");
            }
            else
            {
                // an int never has a leading zero, so any zero found is not the first digit
                boolean duck = false;           // true if the digit 0 is present
                if (n == 0)                     // the only digit of 0 is the digit zero
                    duck = true;
                int t = n;                      // copy of n, so that n is not destroyed
                while (t > 0)
                {
                    int d = t % 10;             // last digit of t
                    if (d == 0)
                        duck = true;
                    t = t / 10;                 // drop the last digit
                }
                if (duck)
                    System.out.println(n + " is a Duck number");
                else
                    System.out.println(n + " is not a Duck number");
            }
        }
        else
            System.out.println("Invalid input");
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the number typed by the user
nintThe number to be checked
duckbooleanTrue if the digit 0 is present in n
tintCopy of n whose last digit is dropped in each pass
dintThe last digit of t in the current pass

Sample input and output

Sample 1

Enter a number: 7056
7056 is a Duck number

Sample 2

Enter a number: 0
0 is a Duck number

Sample 3

Enter a number: 1234
1234 is not a Duck number

Sample 4

Enter a number: -25
Invalid input

One value per line

Dry run

Input: 7056, so n = 7056. The loop runs with t = n.

Steptd = t % 10Action
Start7056-n is not 0, so duck = false and t = 7056
170566Not 0, so t = 705
27055Not 0, so t = 70
3700Zero found, duck = true, t = 7
477Not 0, so t = 0 and the loop stops

duck is true, so the output is 7056 is a Duck number, the same as the real run above.

Variations

  1. Range "Accept two numbers m and n and display all the Duck numbers between them, with their count."
  2. Zero count "Accept a number and count the zero digits in it."
  3. Recursive technique "Check a Duck number using a method that calls itself."