CodersZone logo

Duck Number

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.

Algorithm

  1. We first take a number
  2. We then find the last digit of the number.
  3. If the last digit is zero, it is a Duck number.
  4. Otherwise, we remove that digit from the number.
  5. Perform steps 2, 3, and 4 until the number becomes zero.

Code

                # 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"
                
                 
            

Output

                It is a duck number
 
See the same program in Java See the same program in C