Open In App

Duck Number Java

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will look at a Duck Number in Java. We will look at its meaning and rules for a number to be Duck Number with the help of examples and write a Java program to demonstrate and verify it through our code.

What is Duck Number?

A Duck Number is a positive non-zero number containing at least one zero in its numeric representation. The zero should be present as a leading zero, i.e. the digit zero can be present at any position except at the start of the number.

Rules for a number to be a Duck Number

  • The number contains at least one zero.
  • The number does not contain the zero at the start.
  • The zeros can be at any position in number except the leading position.

Examples of Duck Numbers

Let us look at some examples of Duck numbers to understand the concept better:

  • 2509 is a Duck number because it contains at least one zero and is not present at the beginning.
  • 0931 is not a Duck number because it has a leading zero.
  • 00777 is not a Duck number as it contains two zeros, but both present as leading zeros.
  • 11005 is a Duck number as it includes two zeros, one at the 3rd position and another at the 4th position, but not at the start.
  • 010902 is not a Duck number; it contains three zeros, but one of them is present at the start of the number.

Approach:

  • Take the input number num as the string datatype.
  • Find the length of the string num and initialize an integer variable zero = 0 to store the zeros in num.
  • Start iterating over the characters in num, and if the character is equal to ‘0’, then increment the zeros variable, marking we encountered a zero in num.
  • Now check if the first digit of this num is non-zero and the count of zeros in num is greater than 0.
  • If the above condition is satisfied, it means num is a Duck Number. Otherwise, num is NOT a Duck Number.

Program to check whether a given number is a Duck Number or not

Below is the Java program to check whether the given number is a Duck Number or not.

Java




// Java Program to check whether the number is Duck number or not
import java.io.*;
import java.util.*;
  
class GFG {
    // Function to check whether given number num is a
    // Duck Number or not
    public static boolean checkForDuckNumber(String num)
    {
        // len stores total number of digits in num
        int len = num.length();
        int zeros = 0;
        
        // below loop counts number of zeros in num
        for (int i = 0; i < len; i++) 
        {
            char digit = num.charAt(i);
            if (digit == '0')
                zeros++;
        }
        
        // get the first digit of num
        char startDigit = num.charAt(0);
        
        // if first digit is non-zero and atleast one zero
        // is encountered, then num is Duck Number
        if (startDigit != '0' && zeros > 0)
            return true;
        
        // if above condition is not satisifed, then num
        // is NOT a Duck Number
        return false;
    }
    public static void main(String[] args)
    {
        String num1 = "0012740";
        boolean isDuckNumber1 = checkForDuckNumber(num1);
        
        if (isDuckNumber1) {
            System.out.println("Given number " + num1
                               + " is a Duck Number");
        }
        else 
        {
            System.out.println("Given number " + num1
                               + " is not a Duck Number");
        }
        
        
        String num2 = "11005";
        boolean isDuckNumber2 = checkForDuckNumber(num2);
        
        if (isDuckNumber2) {
            System.out.println("Given number " + num2
                               + " is a Duck Number");
        }
        else 
        {
            System.out.println("Given number " + num2
                               + " is not a Duck Number");
        }
    }
}


Output

Given number 0012740 is not a Duck Number
Given number 11005 is a Duck Number


Explanation of the above Program:

  • The above code has a function named checkForDuckNumber() that takes the string representation of a number and checks whether the number is a Duck Number.
  • We iterate over all digits of the number and count the number of zeros we encounter.
  • Then we check if the first digit is non-zero and the count of zeros is greater than 0; then it is a duck number. Otherwise, it is NOT a Duck Number.

Complexity of the above Program:

Time Complexity: O(D)
Space Complexity: O(D)

D is the total number of digits in the number



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads