Duck Number Program in Java and Python
Duck Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a number and check whether it is a Duck Number or not.
Duck Number: A Duck Number is a number which contains at least one zero, but zero should not be present at the beginning of the number. For example, 3210, 7056 and 8430709 are Duck Numbers, whereas 08237 and 04309 are not Duck Numbers.
Algorithm:
Step 1: Start.
Step 2: Accept the number as a string.
Step 3: Store the first character of the string.
Step 4: Initialize a counter to count zeroes after the first digit.
Step 5: Read each character from the second position to the end of the string.
Step 6: If the character is '0', increase the counter.
Step 7: If the counter is greater than 0 and the first character is not '0', display that it is a Duck Number; otherwise, display that it is not a Duck Number.
Step 8: Check the first character separately to reject numbers beginning with zero.
Step 9: Scan remaining character positions one by one for digit 0.
Step 10: Use a flag or counter to remember whether a zero has appeared after the first digit.
Step 11: Stop.
Explanation:
A duck number is a number that contains at least one zero, but the zero must not be the first digit. That is why the program treats the input as a string.
Using a string allows the program to inspect each digit without losing leading zeroes. A number beginning with zero is rejected because leading zeroes are not allowed in a duck number.
The program scans the characters from the second position onward and checks whether any character is 0. A counter or flag records whether such a zero has been found.
If a zero appears after the first digit, the number is a duck number. If no such zero appears, or if the first digit itself is zero, it is not accepted as a duck number.
A Duck number is identified by the presence of zero in the number, but leading zeros are not counted because they do not change the numeric value. The program therefore has to search for zero digits inside the number or its string form. If numeric extraction is used, each digit is obtained using modulus 10 and checked against zero. If string scanning is used, each character is compared with 0. The result depends on finding at least one valid zero after the number has begun.
Java Program:
/**
* The class DuckNumber accepts a number as a string and checks
* whether it is a Duck Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class DuckNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
String n = sc.nextLine();
int len = n.length();
int count = 0;
/*
* Start from index 1 because a zero at the beginning
* is not allowed for a Duck Number.
*/
for(int i = 1; i < len; i++)
{
if(n.charAt(i) == '0')
count++;
}
/*
* The number must contain at least one zero after the first digit,
* and the first digit itself must not be zero.
*/
if(count > 0 && n.charAt(0) != '0')
System.out.println(n + " is a Duck Number");
else
System.out.println(n + " is not a Duck Number");
}
}Equivalent Python Program:
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.
n = input("Enter a number: ")
count = 0
# Start from index 1 because a starting zero is not allowed.
for i in range(1, len(n)):
if n[i] == '0':
count = count + 1
# A Duck Number has at least one zero, but not at the beginning.
if count > 0 and n[0] != '0':
print(n, "is a Duck Number")
else:
print(n, "is not a Duck Number")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.