# Duck Number in Java

A number that has at least one 0 ( but not at the beginning of the number ) is called a duck number. In this post we will develop a Java program to check the given number is a duck number or not.

Example of the duck number:-
102350, it has two 0 so it is a duck number.
0123, it has one 0 but at the beginning of the number, so it is not a duck number.
078050, it has 3 zeros and among them, two are not at the beginning so it is a duck number.

Procedure to check the given number is duck number or not
1) Take a number
2) Find the last digit of the number
3) If the last digit is 0 then it is a duck number
4) Remove the last digit of the number
5) Repeat the 2 to 4 step until the number becomes 0

``````import java.util.Scanner;

public class DuckNumber {

// method to check duck number
public static boolean isDuck(int number) {

// loop to repeat the process
while(number != 0) {

// check last digit
if(number%10 == 0)
return true;

// remove last digit
number /= 10;
}

// else it is not a duck number
return false;
}

public static void main(String[] args) {

// declare variables
int number = 0;

Scanner scan =  new Scanner(System.in);
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check the number is duck number or not
if(isDuck(number))
System.out.println(number+" is a"
+ " duck number");
else
System.out.println(number+" is not a"
+ " duck number");

// close Scanner class object
scan.close();
}
}``````

The output for the different test-cases are:-

Enter an integer number:: 0123
123 is not a duck number

Enter an integer number:: 012023
12023 is a duck number

Enter an integer number:: 980750
980750 is a duck number

## Java program to check duck number using String

We can also use the String class to perform this operation

Procedure to check the given number is duck number or not using String

1) Take a number
2) Convert it into String
3) Check the first character is zero or not? If yes then it is not a duck number
4) Check every character of the string if any character is 0 then it is a duck number.

``````import java.util.Scanner;

public class DuckNumber {

// method to check duck number using String
public static boolean isDuck(int number) {

// convert number into string
String str = Integer.toString(number);

// check first character
if(str.charAt(0)=='0') return false;

// iterate through remaining characters
for(int i=1; i<str.length(); i++) {
if(str.charAt(i)=='0')
return true;
}

return false;
}

public static void main(String[] args) {

// declare variables
int number = 0;

Scanner scan =  new Scanner(System.in);
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check the number is duck number or not
if(isDuck(number))
System.out.println(number+" is a"
+ " duck number");
else
System.out.println(number+" is not a"
+ " duck number");

// close Scanner class object
scan.close();
}
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!