Program to Check Duck Number

Python Program to Check Duck Number

In the previous article, we have discussed Python Program to Convert Decimal Number to Binary String using Recursion
Duck Number:

A Duck number is one that has a zero in between or at the end of a given number.

For Example:

Let Number = 2300

It is a Duck number because it has zeros to the end.

Number= 00054

It is not a Duck number because it has zeros at the beginning. The actual value of a number is 54 if those zeros are removed.

Given a number and the task is to check if the given number is a Duck Number or not.

lstrip() Method in Python:

The lstrip() method returns a string copy with the leading characters removed (based on the string argument passed).

Based on the argument, lstrip() removes characters from the left (a string specifying the set of characters to be removed).

Examples:

Example1:

Input:

Given Number = 320045

Output:

The above given number { 320045 } is a Duck number

Example2:

Input:

Given Number = 1400

Output:

The above given number { 1400 } is a Duck number

Program to Check Duck Number in Python

Below are the ways to check if the given number is a Duck Number or not:

Method #1: Using lstrip() Method (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number to a string using the str() function and store it in another variable.
  • Apply the lstrip() method with the argument passed as 0 on the above string number and store it in another variable.
  • Check if 0 is present in the above lstrip number using the if conditional statement.
  • If the statement is true, then print “The given number is a Duck Number”.
  • If the statement is false, print “The given number is Not a Duck Number”.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 320045
# Convert the given number to a string using the str() function and
# store it in another variable.
str_numb = str(gvn_numb)
# Apply the lstrip() method with the argument passed as 0 on the above string number
# and store it in another variable.
lstrip_num = str_numb.lstrip("0")
# Check if 0 is present in the above lstrip number using the if conditional statement.
if("0" in lstrip_num):
  # If the statement is true, then print "The given number is a Duck Number".
    print("The above given number {", gvn_numb, "} is a Duck number")
else:
  # If the statement is false, print "The given number is Not a Duck Number".
    print("The above given number {", gvn_numb, "} is Not a Duck number")

Output:

The above given number { 320045 } is a Duck number

Method #2: Using lstrip() Method (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Convert the given number to a string using the str() function and store it in another variable.
  • Apply the lstrip() method with the argument passed as 0 on the above string number and store it in another variable.
  • Check if 0 is present in the above lstrip number using the if conditional statement.
  • If the statement is true, then print “The given number is a Duck Number”.
  • If the statement is false, print “The given number is Not a Duck Number”.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some random Number = "))
# Convert the given number to a string using the str() function and
# store it in another variable.
str_numb = str(gvn_numb)
# Apply the lstrip() method with the argument passed as 0 on the above string number
# and store it in another variable.
lstrip_num = str_numb.lstrip("0")
# Check if 0 is present in the above lstrip number using the if conditional statement.
if("0" in lstrip_num):
  # If the statement is true, then print "The given number is a Duck Number".
    print("The above given number {", gvn_numb, "} is a Duck number")
else:
  # If the statement is false, print "The given number is Not a Duck Number".
    print("The above given number {", gvn_numb, "} is Not a Duck number")

Output:

Enter some random Number = 000897
The above given number { 897 } is Not a Duck number

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.