[2168 views]
A positive non-zero number is said to be a duck number if it contains zero in it. For example: 450,1089, 78092, 205, etc.
Let us consider the number 1087:
1087 is a duck number as it contains zero in the second position.
Let us consider the number 00245:
00245 is not a duck number as it has two leading zeros, which has no meaning.
In this article, we will learn about the algorithm and flowchart to check whether a given number is duck number or not, followed by a short explanation.
In this problem, we need to check whether a given number is a duck number or not. To do so, we need to check whether the number contains zero or not, keeping in mind that any leading zeros will not be considered.
The algorithm starts off by taking the number to be checked as user input, say ‘n’. We then initialize a flag as false. This will be used to do the final checking. We start a loop that runs until n is not equal to zero. In each iteration of this loop, we extract the last digit of the number by doing: n % 10 and check whether that digit is equal to zero or not. If a zero is found, the flag is set to true and we come out of the loop. When the loop completes its execution, if the flag is true, we display “duck number”, else, there were no zeros in the number, we display “Not a duck number”.
Note: Here ‘%’ is the modulus operator which returns the remainder value after division.