Open In App
Related Articles

Check Whether a number is Duck Number or not

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A Duck number is a positive number which has zeroes present in it, For example 3210, 8050896, 70709 are all Duck numbers. Please note that a numbers with only leading 0s is not considered as Duck Number. For example, numbers like 035 or 0012 are not considered as Duck Numbers. A number like 01203 is considered as Duck because there is a non-leading 0 present in it.

Examples :

Input : 707069 
Output : It is a duck number. 
Explanation: 707069 does not contains zeros at the beginning.

Input : 02364 
Output : It is not a duck number. 
Explanation: in 02364 there is a zero at the beginning of the number.

Recommended Practice

Implementation:

C++


                    

Java

// Java Program to check whether a
// number is Duck Number or not.
 
import java.io.*;
class GFG {
 
    // Function to check whether
    // the given number is duck number or not.
    static boolean check_duck(String num)
    {
        // Ignore leading 0s
        int i = 0, n = num.length();
        while (i < n && num.charAt(i) == '0')
            i++;
 
        // Check remaining digits
        while (i < n) {
            if (num.charAt(i) == '0')
                return true;
            i++;
        }
 
        return false;
    }
 
    // Driver Method
    public static void main(String args[]) throws IOException
    {
        String num = "1023";
        if (check_duck(num))
            System.out.println("It is a duck number");
        else
            System.out.println("It is not a duck number");
    }
}

                    

Python

# Python program to check whether a number is Duck Number or not.
 
# Function to check whether
# the given number is duck number or not.
def check_duck(num) :
 
    # Length of the number(number of digits)
    n = len(num)
    
    # Ignore leading 0s
    i = 0
    while (i < n and num[i] == '0') :
        i = i + 1
 
    # Check remaining digits
    while (i < n) :
        if (num[i] == "0") :
            return True
        i = i + 1
 
    return False
     
 
# Driver Method
num1 = "1023"
if(check_duck(num1)) :
    print "It is a duck number"
else :
    print "It is not a duck number"
 
# Write Python3 code here

                    

C#

// C# Program to check whether a
// number is Duck Number or not.
using System;
 
class GFG {
     
    // Function to check whether
    // the given number is duck
    // number or not.
    static bool check_duck( String num)
    {
         
        // Ignore leading 0s
        int i = 0, n = num.Length;
        while (i < n && num[i] == '0')
            i++;
 
        // Check remaining digits
        while (i < n) {
            if (num[i] == '0')
                return true;
            i++;
        }
 
        return false;
    }
     
 
    // Driver Method
    public static void Main()
    {
         
        String num1 = "1023";
         
        // checking number1
        if( check_duck(num1))
            Console.Write("It is a "
                      + "duck number");
        else
            Console.Write("It is not "
                    + "a duck number");
    }
}
 
// This code is contributed by
// nitin mittal.

                    

Javascript

<script>
 
// Javascript program to check whether
// a number is Duck Number or not.
 
// Function to check whether
// given number is duck number or not.
function check_duck(num)
{
     
    // Ignore leading 0s
    let i = 0, n = num.length;
    while (i < n && num[i] == '0')
        i++;
 
    // Check remaining digits
    while (i < n)
    {
        if (num[i] == '0')
            return true;
             
        i++;
    }
    return false;
}
 
// Driver code
let num = "1023";
 
if (check_duck(num))
    document.write("It is a duck number");
else
    document.write("It is not a duck number");
     
// This code is contributed by rishavmahato348
 
</script>

                    

Output
It is a duck number




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

Approach 2:String manipulation

  1. Take the input number as an integer n.
  2. Convert the integer n to a string s using the to_string function in C++.
  3. Find the length of the string s and initialize a boolean variable hasZero to false.
  4. Iterate over the characters of the string s starting from the second character (i.e., the first non-zero digit) to the end of the string.
  5. If any character is equal to ‘0’, set hasZero to true and break out of the loop.
  6. Check if the first character of the string s is not equal to ‘0’.
  7. If both conditions in steps 4 and 5 are satisfied, return true (i.e., the number is a Duck Number); otherwise, return false (i.e., the number is not a Duck Number).

C++

#include <iostream>
#include <string>
using namespace std;
 
bool isDuckNumber(int n) {
    string s = to_string(n);
    int len = s.length();
    bool hasZero = false;
    for (int i = 1; i < len; i++) {
        if (s[i] == '0') {
            hasZero = true;
            break;
        }
    }
    return (hasZero && s[0] != '0');
}
 
int main() {
    int n = 1023;
    if (isDuckNumber(n)) {
        cout << "It is a Duck Number" << endl;
    } else {
        cout << "It is not a Duck Number" << endl;
    }
    return 0;
}

                    

Java

import java.util.*;
 
public class Main {
  public static boolean isDuckNumber(int n) {
    String s = Integer.toString(n);
    int len = s.length();
    boolean hasZero = false;
    for (int i = 1; i < len; i++) {
      if (s.charAt(i) == '0') {
        hasZero = true;
        break;
      }
    }
    return (hasZero && s.charAt(0) != '0');
  }
  public static void main(String[] args) {
    int n = 1023;
    if (isDuckNumber(n)) {
      System.out.println("It is a Duck Number");
    } else {
      System.out.println("It is not a Duck Number");
    }
  }
 
}

                    

Python3

def isDuckNumber(n):
    s = str(n)
    len_s = len(s)
    hasZero = False
    for i in range(1, len_s):
        if s[i] == '0':
            hasZero = True
            break
    return (hasZero and s[0] != '0')
 
if __name__ == '__main__':
    n = 1023
    if isDuckNumber(n):
        print("It is a Duck Number")
    else:
        print("It is not a Duck Number")

                    

C#

using System;
 
public class GFG{
    // Function to check if a number is a Duck Number
    public static bool isDuckNumber(int n){
        string s = n.ToString(); // Convert the number to a string for easier manipulation
        int len = s.Length; // Get the length of the string representation of the number
        bool hasZero = false; // Variable to track if the number has a zero digit (other than leading zeros)
         
        // Loop through each digit of the number, starting from the second digit (index 1)
        for (int i = 1; i < len; i++){
            // If a zero digit is found (other than the leading zero), set hasZero to true and exit the loop
            if (s[i] == '0'){
                hasZero = true;
                break;
            }
        }
        // A number is considered a Duck Number if it has a zero digit (other than the leading zero)
        // and the first digit is not zero. Return true if it is a Duck Number, false otherwise.
        return (hasZero && s[0] != '0');
    }
 
    public static void Main(string[] args){
        int n = 1023; // Sample number to check if it is a Duck Number
        // Call the isDuckNumber function to check if the number is a Duck Number
        if (isDuckNumber(n)){
            Console.WriteLine("It is a Duck Number");
        }
        else{
            Console.WriteLine("It is not a Duck Number");
        }
    }
}
// THIS CODE IS CONTRIBUTED BY KANCHAN AGARWAL

                    

Javascript

function isDuckNumber(n) {
    const s = n.toString();
    const len = s.length;
    let hasZero = false;
    for (let i = 1; i < len; i++) {
        if (s[i] === '0') {
            hasZero = true;
            break;
        }
    }
    return hasZero && s[0] !== '0';
}
 
const n = 1023;
if (isDuckNumber(n)) {
    console.log("It is a Duck Number");
} else {
    console.log("It is not a Duck Number");
}
 
// THIS CODE IS CONTRIBUTED BY KANCHAN AGARWAL

                    

Output
It is a Duck Number




Time Complexity: O(n) 
Auxiliary Space: O(n)

The time complexity of this algorithm is O(n), where n is the number of digits in the input integer. This is because we need to iterate over all the digits in the integer to check if there is a zero after the first non-zero digit.

The space complexity of this algorithm is also O(n), because we need to store the string representation of the input integer. The length of this string is equal to the number of digits in the integer, which is at most log10(n) + 1.

In general, string manipulation algorithms have higher space complexity compared to their integer-based counterparts because they require additional space to store the string representation of the input. However, they can sometimes be more intuitive to understand and implement.

 



Last Updated : 09 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads