Updated April 6, 2023
Definition of Duck Number
Duck number is that positive number that has at least one zero present in it, excluding zeros present in high order or leading positions of the number. Zeroes that add value to the number, should only be considered for duck number computation. Zeroes present in the Leading or high order positions in the number do not add any value and hence they are omitted.
Duck numbers might find some applications in Statistics, games, data analysis, and data science areas. We should have a simple and fast method to compute them to make these applications attractive to the users.
Logic behind
Some Samples are given below
- 87292 is not a duck number since it does not have any zeroes in it
- 00087 is not a duck number because zeroes are present only in insignificant positions, which do not add any value
- 089 is not a duck number for the same reason
- A mere 0 is not a duck number
- 9032 is a duck number since it adds value to the number
- 80 is a duck number for the same reason
- -130 is not a duck number since the number is negative
- It should not contain any decimal value
How to compute Duck Number?
The following steps are used to compute.
- Accept the number. It should be a positive integer number
- Check for zeroes in the high order or leading positions that are insignificant and remove them.
- Hence the first digit in the number should not be zero
- Scan thru the number and find out whether any zeroes are present in the number.
- If so declare the number as a duck number otherwise, it is not a duck number
Example in C++ Language
A sample program was developed in C++ language to determine whether a given number is a Duck number or not.
Code:
// C++ program to determine whether a given number is a duck number or not
#include <iostream> // C++ mandatory lines
using namespace std;
int check_duck(string num) // Function takes the number supplied and
//check for duck number
{
int i = 0, n = num.length(); // Initilise variables and find out the length of
int x=0; // the number string
// Starting the main loop that scan thru the number end to end for the entire length
// (o to length)
while (i < n) {
if (num[i] != '0') // Scanning leading zeroes and
x = 1; // set the onset of the non-zero
if (num[i] == '0' and x ==1) // Check for zeroes after hitting on a non zero
return 1; //value . If so return a value 1 back
i++; // increment index
}
}
int main(void) // First execution line. Starting point
{
string num = ""; // Define a string variable to store the number
cout << "Enter the number Please:"; // Accept the number
cin >> num;
int result2; // Initialise variable
result2 = check_duck(num); // Call the function
if (result2 == 1) // infer the result and display the result
cout << " " << num <<" is a duck number" ;
else
cout << " "<< num << " is not a duck number";
}
Result for the various values
Program logic
- The number is accepted as a string
- Number String is scanned from left to right character by character
- The first occurrence of a non-zero number is established.
- Post this non-zero occurrence if any zero is encountered the number is declared as a duck number otherwise not.
Example in Python
A sample program is developed in Python language using the same logic and the program code is:
# Python program to determine whether a given number is a duck number or not
s=0 # Defining and initialising the program variables
k=0
x = input("Your Number Please : ") # Prompting for number and taking it in x
for i in x: # Scanning character by character (left to right)
if i != "0": # Hit upon first non zero number from left
k = 1 # Capturing the occurrence of first non-zero
if i == "0" and k == 1: # looking out from next zero character if it is
s = 1 # followed by a non-zero
# sensing that event and set a switch as result
if s==1: # interpreting the result using the switch
#and display whether the number is duck or not
print (f" The given number {x} is a duck number")
if s==0:
print (f" The given number {x} is not a duck number")
The result for various inputs are given
Logic used
- Number is accepted as a string
- Numbers are scanned character by character from left to right
- Zeroes in the high order or leading positions are weeded out and the first non-zero character is sensed.
- The Post-non-zero presence of zero characters is sensed and the result is inferred.
Conclusion
We have understood the meaning and importance of duck numbers and also the ways to compute them through python and C++ programs. Using this logic one can easily find out the duck numbers in a given range of numbers and incorporate them in their game or data science applications.
Recommended Articles
We hope that this EDUCBA information on “Duck Number” was beneficial to you. You can view EDUCBA’s recommended articles for more information.