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

ISC Practicals and Theory - Duck Number (OOP)

Updated 20 Oct 2026

A Duck number is a number in which the digit zero is present, but not as its first digit. This version writes the check as a class Duck whose constructor takes the number, counts the zero digits with % 10 and / 10, and decides from that count. main only reads the number, creates an object and calls display().

Definition

A Duck number contains the digit 0, but its first digit is not 0. A number stored as an int has no leading zero, so for such a number it is enough that the digit 0 is present.

  • $3210$, $7056$, $100$ and $1203$ are Duck numbers.
  • $123$ and $4567$ are not Duck numbers: they have 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".

Algorithm

  1. main reads the number into m. If the input is not an integer, it prints "Invalid input".
  2. main creates the object ob with new Duck(m). The constructor stores m in the data member n and sets zeros = 0.
  3. main calls ob.display(). If n is negative it prints "Invalid input". Otherwise it calls:
    • countZeros(): copy n into t, then repeat: if t % 10 is 0, add 1 to zeros; then t = t / 10; continue while t > 0. A do-while loop is used so that the single digit of 0 is also examined.
    • isDuck(): returns true if zeros > 0.
  4. display() prints "is a Duck number" or "is not a Duck number".

Program

The constructor stores the number, countZeros() counts the zero digits, isDuck() decides, and display() prints.

import java.util.*;

/* Class Duck stores a number and checks whether it is a Duck number
   (the digit 0 is present in it, but not as its first digit). */
public class Duck
{
    int n;       // the number to be checked
    int zeros;   // how many zero digits n has

    // Constructor: stores the number and sets the zero count to 0
    // Parameter: x - the number typed by the user
    Duck(int x)
    {
        n = x;
        zeros = 0;
    }

    // Counts the zero digits of n by taking out the digits from the right
    // (do-while, so that the single digit of 0 is also examined)
    void countZeros()
    {
        int t = n;              // copy of n, so that n is not destroyed
        zeros = 0;
        do
        {
            if (t % 10 == 0)    // last digit of t is zero
                zeros++;
            t = t / 10;         // drop the last digit
        } while (t > 0);
    }

    // Decides the Duck number condition from the zero count
    // Returns: true if n has at least one zero digit
    boolean isDuck()
    {
        return zeros > 0;
    }

    // Prints the result
    void display()
    {
        if (n < 0)
            System.out.println("Invalid input");
        else
        {
            countZeros();
            if (isDuck())
                System.out.println(n + " is a Duck number");
            else
                System.out.println(n + " is not a Duck number");
        }
    }

    // main: reads the number, creates the object and calls the method
    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 m = sc.nextInt();              // the number typed by the user
            Duck ob = new Duck(m);             // object of class Duck
            ob.display();
        }
        else
            System.out.println("Invalid input");
    }
}

Variable description table

VariableData TypePurpose
nintData member: the number to be checked
zerosintData member: how many zero digits n has
xintParameter of the constructor: the number to be stored
tintCopy of n from which the digits are taken out one by one
argsString[]Command line arguments of main (not used)
scScannerReads the input from the keyboard
mintThe number typed by the user
obDuckObject of class Duck created in main

Sample input and output

Sample 1

Enter a number: 1203
1203 is a Duck number

Sample 2

Enter a number: 0
0 is a Duck number

Sample 3

Enter a number: 4567
4567 is not a Duck number

Sample 4

Enter a number: -25
Invalid input

One value per line

Dry run

Input: 1203, so the constructor sets n = 1203 and zeros = 0. countZeros() runs with t = 1203.

Steptt % 10zeros after the stept after t / 10
1120330120
21200112
312211
41110

t is now 0, so the loop stops with zeros = 1. isDuck() returns true because zeros > 0, and display() prints 1203 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."