Duck number in Java
In this tutorial, we will understand what is meant by the Duck number in Java and understand it through Java Program.
In programming, a duck number is a positive number that contains zeroes in it, but not at the beginning.
Example -
- 101 is a duck number since it contains a zero in the middle.
- 2345606 is a duck number since it contains a zero in the middle.
- 30303 is a duck number since it contains a zero in the middle.
- 0456037 is a duck number since it contains a zero not only in the beginning but also in the middle.
- 01011 is a duck number since it contains a zero not only in the beginning but also in the middle.
- 56770 is a duck number since it contains a zero at the end but not at the beginning.
- 015110 is a duck number since it contains a zero not only in the beginning but also at the end.
- 04567 is not a duck number since it contains a zero only at the beginning.
- 0863 is not a duck number since it contains a zero only at the beginning.
- 0875 is not a duck number since it contains a zero only at the beginning.
Algorithm:
- Create a class named "Duck_Number".
- Create a static method "isDuck" that takes a string parameter "num" and returns a boolean value. This method will check if the given number is a duck number or not.
- In the "isDuck" method, initialize two variables "i" and "n" to 0 and the length of the string "num", respectively.
- Using a while loop, drop the leading zeroes from the given number.
- Using another while loop, check the remaining digits of the number.
- If the first non-zero digit encountered is zero, then the number is a duck number. Return true.
- If no zeroes are found in the remaining digits, then the number is not a duck number. Return false.
- In the main method, create a Scanner object "sc" to read the input from the user.
- Print a message asking the user to enter a number.
- Read the input number as a string using the nextLine method of the Scanner object.
- Call the "isDuck" method with the input number as the argument.
- If the returned value is true, then print "It is a duck number".
- If the returned value is false, then print "It is not a Duck number”.
Implementation:
File name: Duck_Number.java
// Java Program to check whether a
// number is Duck Number or not.
import java.io.*;
import java.util.*;
class Duck_Number {
// function to check if
// the entered number is duck number or not?
static boolean isDuck(String num)
{
// dropping leading zeroes
int i = 0, n = num.length();
while (i < n && num.charAt(i) == '0')
i++;
// Check the left over digits
while (i < n) {
if (num.charAt(i) == '0')
return true;
i++;
}
return false;
}
// main Method
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any no.:");
String num = sc.nextLine();
if (isDuck(num))
System.out.println("It is a duck number");
else
System.out.println("It is not a duck number");
}
}
Output 1:

Output 2:

Output 3:

Output 4:

Complexity Analysis:
Time Complexity: O(n) where n is the length of the string.
Space Complexity: O (1)