A Duck number is a unique type of positive non-zero number that includes the digit zero within it. However, it is crucial to note that the digit zero should not appear at the beginning of the number. It can be positioned at any other place within the number.
Let’s explore a few examples of Duck numbers:
- The number 3210 is a Duck number since it contains a zero at the end, but not at the beginning.
- However, the number 08237 is not a Duck number because it contains a zero at the beginning, violating the condition.
- On the other hand, the number 7033 is a Duck number as it has zero in the second position, meeting the requirements.
- The number 030405 is not a Duck number because it contains a zero at the beginning, which is not allowed.
- Similarly, the number 00153 is also not a Duck number as it contains leading zeros, which is against the rule.
To determine whether a given number is a Duck number or not, we can follow the steps outlined below:
- Take the input number.
- Find the last digit of the number.
- If the last digit is zero, then the number is a Duck number.
- If the last digit is not zero, remove that digit from the number.
- Repeat steps 2, 3, and 4 until the number becomes zero.
- If, at any point, the number has a remaining non-zero digit, then it is not a Duck number.
Let’s implement the code to check whether it is a Duck number or Not.
DuckNumber.java
//import required classes and packages import java.util.*; import java.io.*; import java.util.Scanner; //create DuckNumberExample class to check whether the given number is a Duck number or not public class DuckNumberExample { // create checkNumber() method that returns true when it founds number Buzz public static boolean checkNumber(int number) { // use loop to repeat steps while(number != 0) { // check whether the last digit of the number is zero or not if(number%10 == 0) return true; //return true if the number is Duck // divide the number by 10 to remove the last digit number = number / 10; } return false; //return false if the number is not Duck } // main() method start public static void main(String args[]) { int n1, n2; //create scanner class object to get input from user Scanner sc=new Scanner(System.in); //show custom message System.out.println("Enter first number"); //store user entered value into variable n1 n1 = sc.nextInt(); //show custom message System.out.println("Enter second number"); //store user entered value into variable n2 n2 = sc.nextInt(); if (checkNumber(n1)) System.out.println(n1 + " is a Duck number"); else System.out.println(n1 + " is not a Duck number"); if (checkNumber(n2)) System.out.println(n2 + " is a Duck number"); else System.out.println(n2 + " is not a Duck number"); } }
Output
FindAllDuckNumber.java
//import required classes and packages import java.util.*; import java.io.*; import java.util.Scanner; //create FindAllDuckNumber class to get all the Duck number in a given range class FindAllDuckNumber { //main() method start public static void main(String args[]) { int range; //create scanner class object Scanner sc=new Scanner(System.in); //show custom message System.out.println("Enter the value of range"); //store user entered value into variable range range = sc.nextInt(); for(int i = 1; i <= range; i++) if(checkNumber(i)){ System.out.println(i + " is a Duck number"); } } // create checkNumber() method that returns true when it founds number Buzz public static boolean checkNumber(int number) { // use loop to repeat steps while(number != 0) { // check whether the last digit of the number is zero or not if(number%10 == 0) return true; //return true if the number is Duck // divide the number by 10 to remove the last digit number = number / 10; } return false; //return false if the number is not Duck } }
Output
To enhance your knowledge, make sure to follow tutorials.freshersnow.com for topics like Duck Number in Java.