# Duck Number in Java

(Last Updated On: 02/06/2023)

In this blog, we’ll learn the meaning of the duck number in Java and other information related to that. So let’s start with the blog on Duck Number in Java.

## What is the Duck Number in Java?

In Java, we define a positive integer as a “duck number” if it has at least one zero but does not have any zeros at the beginning. In other words, a duck number is a number that contains the digit “0” and we can write it as a string.

### Examples of duck numbers in Java include:

102- This number possesses a ‘0’ digit but does not have a ‘0’ preceding it.

203 – This number also has a ‘0’ digit but does not begin with a ‘0’.

5008 – It contains multiple ‘0’ digits but does not have a ‘0’ at the beginning.

909 – Despite containing a ‘0,’ this number begins with a ‘9’ and is still considered a Duck Number.

These examples demonstrate the concept of Duck Numbers in Java, emphasizing the presence of a ‘0’ digit and the absence of a ‘0’ at the start.

Get a Complete list of Online Certification Courses in India here!

## Java Program to check whether a given number is a Duck number or not in 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 DuckNumEx {

// create checkNumber() method that returns true when it founds number Buzz
public static boolean checkNumber(int n) {

// use loop to repeat steps
while(n != 0) {

// check whether the last digit of the number is zero or not
if(n%10 == 0)
return true;    //return true if the number is Duck

// divide the number by 10 to remove the last digit
n = n / 10;
}

return false; //return false if the number is not Duck
}
// main() method start
public static void main(String args[])
{
int num;

//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
num = sc.nextInt();

if (checkNumber(num))
System.out.println(num + " is a Duck number");
else
System.out.println(num + " is not a Duck number");

}
} ```

Explanation f the code:

1. Import Required Classes and Packages: To utilize the Scanner class and manage input/output operations, we import the required classes and packages.
2. Create the DuckNumberExample Class: We create the DuckNumberExample class to contain the code logic.
3. Define the checkNumber() Method: The parameter ‘n’ in the checkNumber() method definition stands for the number that needs to be verified. The boolean value returned by this method indicates whether the supplied number is a Duck number or not.
4. Use a Loop: We utilize a while loop to repeat the steps until the number reduces to zero.
5. Check the Last Digit: Inside the loop, the code checks whether the number’s last digit is zero using the condition n% 10 == 0.
6. Return True or False: The method returns true if the last digit is zero, indicating that the number is a Duck number. Otherwise, it returns false. If not, the last digit is eliminated by dividing the number by 10, and the loop is repeated until the number is zero.
7. Implement the main() method: We employ the main() method to handle user input and invoke the checkNumber() method.
8. Get User Input: A Scanner object is created to read the user’s input.
9. Input Prompt: A message appears asking the user to enter a number.
10. Call the checkNumber() Method: The ‘num’ variable stores the user’s input, and the checkNumber() method is invoked with ‘num’ as the argument.
11. Display the Result: Based on the returned boolean value, the program prints whether or not the given number is a Duck number.

This Java code enables users to rapidly determine whether a given number is a Duck number or not by utilizing the logic and functionality of the checkNumber() method.

Also, learn about Spy Number in Java here!

#### Output:

``````Enter first number
3210
3210 is a Duck number``````

## Find all Duck numbers in given range

```//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 FindAllDuckNum
{
//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 num) {

// use loop to repeat steps
while(num != 0) {

// check whether the last digit of the number is zero or not
if(num%10 == 0)
return true;    //return true if the number is Duck

// divide the number by 10 to remove the last digit
num = num / 10;
}

return false;   //return false if the number is not Duck
}
}  ```

Explanation of the code:

The given code is an implementation in Java to find all the Duck numbers within a given range. Here’s an explanation of the code:

1. Import of Required Classes and Packages: To utilize the Scanner class and manage input/output operations, you need to import the necessary classes and packages.
2. Create the FindAllDuckNum Class: We create the FindAllDuckNum class to contain the code logic.
3. Implement the main() method: We utilize the main() method to handle user input and iterate through the range of numbers.
4. Obtain User Input: We create a Scanner object to read the user’s input.
5. Input Prompt: A message appears prompting the user to enter the range value.
6. the checkNumber() method should be used: The ‘range’ variable holds the user’s input. To iterate through all the numbers from 1 to the specified range, a for loop is used. The checkNumber() method is used to determine whether each number is a Duck number.
7. Check the Last Digit: A while loop is used inside the checkNumber() method to iterate through the steps until the number equals zero. Using the condition num% 10 == 0, the code determines if the number’s last digit is zero.
8. Return True or False: If the last digit is zero, the method returns true, indicating that the number is a Duck number. Otherwise, it returns false. If not, the last digit is subtracted by 10 before the loop is repeated until the number is zero.
9. Display the Result: If the checkNumber() method returns true for a number in the range, it is considered a Duck number and is printed to the console.

Using the logic and functionality of the checkNumber() method, this code in Java allows users to find and display all Duck numbers within a given range.

Check out our blog on Buzz Number in Java here!

#### Output:

``````Enter the value of range 60
10 is a Duck number
20 is a Duck number
30 is a Duck number
40 is a Duck number
50 is a Duck number
60 is a Duck number``````

## 10 Tips and Tricks for Effective Implementation of Duck Number in Java

1. Understand the Definition of Duck Numbers:

Explain that positive integers called “duck numbers” have a zero in each digit but are not divisible by 10.

Take some time to plan your approach before diving into coding. Define the scope of your program and the procedures for checking for Duck Numbers.

1. Use a Modular Approach:

Break the problem down into smaller, more manageable functions or methods. Modular code is simpler to read, comprehend, and debug.

1. Utilize the Power of Loops:

To iterate through a number’s digits, use loops such as the while loop. Examine each digit to see if it is zero.

1. Implement Input Validation:

To ensure valid input, handle user input effectively. Make sure the input is a positive integer.

1. Optimize for Performance:

Improve the performance of your DuckNumber implementation by using efficient algorithms and techniques. When designing your code, keep time and space complexity in mind.

While running your code, take into account any potential mistakes or exceptions. To handle these circumstances graciously, use the proper error-handling techniques, such as try-catch blocks.

1. Test Extensively:

To confirm its accuracy, test your code using a variety of input circumstances. Your test suite should cover both common and edge scenarios.