- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Conditional Constructs in Java
Chapter 8
Conditional Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: To Check Whether the Number is a Duck Number
29. Create a program in Java to find out if a number entered by the user is a Duck Number.
A Duck Number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example, 6710, 8066, 5660303 are all duck numbers whereas 05257, 080009 are not.
🦆 What is a Duck Number?
A Duck Number:
- Contains at least one zero (0)
- Does NOT start with zero
Examples
✔ Duck Numbers: 6710, 8066, 5660303
✘ Not Duck Numbers: 05257, 080009
import java.util.Scanner;
public class DuckNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num;
boolean hasZero = false;
System.out.print("Enter a number: ");
num = sc.nextLine();
// Check if the number starts with 0
if (num.charAt(0) == '0') {
System.out.println(num + " is NOT a Duck Number");
return;
}
// Check for presence of zero in the number
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) == '0') {
hasZero = true;
break;
}
}
if (hasZero)
System.out.println(num + " is a Duck Number");
else
System.out.println(num + " is NOT a Duck Number");
}
}
Output
Sample Run 1 Enter a number: 8066 8066 is a Duck Number Sample Run 2 Enter a number: 05257 05257 is NOT a Duck Number Sample Run 3 Enter a number: 12345 12345 is NOT a Duck Number
✅ Explanation
- Input is taken as a String to easily check digits.
- If the first character is ‘0’, the number is immediately rejected.
- The program scans the entire number to check for at least one zero.
- If found → Duck Number.
