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
- Read the input. If it is not an integer, or it is negative, print
Invalid inputand stop. (sc.hasNextInt()tells whether the input is an integer.) - Set
duck = false. Ifnis 0, its only digit is the digit zero, so setduck = true. - Copy
nintotso thatnis not destroyed. Whilet > 0:- find the last digit
d = t % 10; - if
d == 0, setduck = true; - drop the last digit with
t = t / 10.
- find the last digit
- An
intnever has a leading zero, so any zero found is not the first digit. Ifduckis true print thatnis 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
n | int | The number to be checked |
duck | boolean | True if the digit 0 is present in n |
t | int | Copy of n whose last digit is dropped in each pass |
d | int | The last digit of t in the current pass |
Sample input and output
Sample 1
Enter a number: 7056
7056 is a Duck numberSample 2
Enter a number: 0
0 is a Duck numberSample 3
Enter a number: 1234
1234 is not a Duck numberSample 4
Enter a number: -25
Invalid inputDry run
Input: 7056, so n = 7056. The loop runs with t = n.
| Step | t | d = t % 10 | Action |
|---|---|---|---|
| Start | 7056 | - | n is not 0, so duck = false and t = 7056 |
| 1 | 7056 | 6 | Not 0, so t = 705 |
| 2 | 705 | 5 | Not 0, so t = 70 |
| 3 | 70 | 0 | Zero found, duck = true, t = 7 |
| 4 | 7 | 7 | Not 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
- 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."
- Recursive technique "Check a Duck number using a method that calls itself."