Algorithm and Flowchart to check whether a given number is a duck number or not

[555 views]




What are duck numbers?

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.

Algorithm to check whether a number is duck number or not:

Step 1: Start Step 2: Read the number from the user, say n Step 3: Initialize flag = false Step 4: Repeat WHILE n!=0: 4.1: IF n%10==0, then: 4.1.a: flag = true 4.1.b: break 4.2: End IF 4.3: n = n/10 Step 5: End WHILE LOOP Step 6: IF flag=true, then: 6.1: Display “Given number is duck number” Step 7: ELSE 7.1: Display “Given number is not a duck number” Step 8: End IF - ELSE Step 9: Stop

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.

Flowchart to check whether a number is duck number or not:

Algorithm and Flowchart to check whether a given number is a duck number or not
Remove WaterMark from Above Flowchart

                 



Having Difficulty understanding above Algorithm and Flowchart? Buy my Personal Notes



Are You Good enough in Algorithms? Prove it!



Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Test Your Skills:

Search Tags

    Duck Number Algorithm

    Flowchart for Duck Number