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
mainreads the number intom. If the input is not an integer, it prints "Invalid input".maincreates the objectobwithnew Duck(m). The constructor storesmin the data membernand setszeros = 0.maincallsob.display(). Ifnis negative it prints "Invalid input". Otherwise it calls:countZeros(): copynintot, then repeat: ift % 10is 0, add 1 tozeros; thent = t / 10; continue whilet > 0. A do-while loop is used so that the single digit of 0 is also examined.isDuck(): returns true ifzeros > 0.
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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: the number to be checked |
zeros | int | Data member: how many zero digits n has |
x | int | Parameter of the constructor: the number to be stored |
t | int | Copy of n from which the digits are taken out one by one |
args | String[] | Command line arguments of main (not used) |
sc | Scanner | Reads the input from the keyboard |
m | int | The number typed by the user |
ob | Duck | Object of class Duck created in main |
Sample input and output
Sample 1
Enter a number: 1203
1203 is a Duck numberSample 2
Enter a number: 0
0 is a Duck numberSample 3
Enter a number: 4567
4567 is not a Duck numberSample 4
Enter a number: -25
Invalid inputDry run
Input: 1203, so the constructor sets n = 1203 and zeros = 0. countZeros() runs with t = 1203.
| Step | t | t % 10 | zeros after the step | t after t / 10 |
|---|---|---|---|---|
| 1 | 1203 | 3 | 0 | 120 |
| 2 | 120 | 0 | 1 | 12 |
| 3 | 12 | 2 | 1 | 1 |
| 4 | 1 | 1 | 1 | 0 |
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
- Range "Accept two numbers m and n and display all the Duck numbers between them, with their count."
- Zero count "Accept a number and count the zero digits in it."