Posted on by Kalkicode
Code Number

# Check whether a number is duck number or not

In this article, we will explore the concept of Duck Numbers and how to determine whether a given number is a Duck Number or not. A Duck Number is a positive number that contains at least one zero and has all its non-leading digits (digits other than the leftmost non-zero digit) as non-zero. For example, the number 10235 is a Duck Number because it has a zero and all other digits are non-zero. On the other hand, 12345 is not a Duck Number as it lacks a zero.

## Problem Statement

The problem is to write a C program that takes a string representation of a number as input and determines whether it is a Duck Number or not. The program should skip any leading zeros and then check if all the remaining digits are non-zero.

## Example

Let's take some test cases to better understand the concept:

1. Input: "529043043" Output: [529043043] is duck number Explanation: The number has a zero (0) and all other digits are non-zero.

2. Input: "0001234" Output: [0001234] is not duck number Explanation: Although the number has a zero (0), the other non-leading digits are not all non-zero.

3. Input: "00012304" Output: [00012304] is duck number Explanation: The number has a zero (0) and all other digits are non-zero.

4. Input: "1000000" Output: [1000000] is duck number Explanation: The number has a zero (0) and all other digits are non-zero.

5. Input: "-1000000" Output: [-1000000] is not duck number Explanation: The given number is not a positive number.

6. Input: "2300200100" Output: [2300200100] is duck number Explanation: The number has a zero (0) and all other digits are non-zero.

## Pseudocode

``````is_duck_number(str_num[], size):
counter = 0
status = 0
wrong = 0

# Skip all the beginning zeros
while counter < size and str_num[counter] == '0':
counter++

# Check if the remaining digits are all non-zero
while counter < size and wrong == 0:
if str_num[counter] >= '0' and str_num[counter] <= '9':
if str_num[counter] == '0':
status = 1
# Terminate loop if we find a zero
else:
wrong = 1
# Set wrong flag if we encounter a non-digit character
counter++

# Determine if the number is a Duck Number or not
if wrong == 0 and status == 1:
print "[str_num] is duck number"
else:
print "[str_num] is not duck number"
``````

## Algorithm Explanation

1. Start by initializing a `counter` variable to keep track of the current position in the string and `status` to 0 (to track if a zero is found).
2. Initialize `wrong` to 0 (to track if an invalid digit is found).
3. Use a `while` loop to skip all the leading zeros in the string.
4. Use another `while` loop to check if the remaining characters are valid digits (0 to 9) and if a zero is found.
5. If a non-digit character is encountered, set `wrong` to 1.
6. After looping through the string, check if `wrong` is still 0 (no invalid digit found) and `status` is 1 (a zero is found). If both conditions are met, the number is a Duck Number.
7. Print the appropriate message based on whether the number is a Duck Number or not.

## Code Solution

Here given code implementation process.

``````// C Program
// Check Whether a number is duck number or not
#include <stdio.h>

// Determine whether given number is duck number or not
void is_duck_number(char str_num[], int size)
{
if (size <= 0)
{
//When given string number is empty
return;
}
int status = 0;
int counter = 0;
//This is used to check valid positive number
int wrong = 0;
// Skip the all beginning zeros
while (counter < size && str_num[counter] == '0')
{
counter++;
}
while (counter < size && wrong == 0)
{
if (str_num[counter] >= '0' && str_num[counter] <= '9')
{
if (str_num[counter] == '0')
{
//We get zero
status = 1;
//Terminate loop when given number always valid number
}
}
else
{
//When not a valid digit
wrong = 1;
}
counter++;
}
if (wrong == 0 && status == 1)
{
printf("\n [%s] is duck number", str_num);
}
else
{
printf("\n [%s] is not duck number", str_num);
}
}
int main()
{
//Test string numbers
char str_num1[] = "529043043";
char str_num2[] = "0001234";
char str_num3[] = "00012304";
char str_num4[] = "1000000";
char str_num5[] = "-1000000";
char str_num6[] = "2300200100";
int size = 0;
//Test case 1
size = sizeof(str_num1) / sizeof(str_num1[0]) - 1;
is_duck_number(str_num1, size);
//Test case 2
size = sizeof(str_num2) / sizeof(str_num2[0]) - 1;
is_duck_number(str_num2, size);
//Test case 3
size = sizeof(str_num3) / sizeof(str_num3[0]) - 1;
is_duck_number(str_num3, size);
//Test case 4
size = sizeof(str_num4) / sizeof(str_num4[0]) - 1;
is_duck_number(str_num4, size);
//Test case 5
size = sizeof(str_num5) / sizeof(str_num5[0]) - 1;
is_duck_number(str_num5, size);
//Test case 6
size = sizeof(str_num6) / sizeof(str_num6[0]) - 1;
is_duck_number(str_num6, size);
return 0;
}``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````/*
Java program
Check Whether a number is duck number or not
*/
class DuckNumber
{
// Determine whether given number is duck number or not
public void is_duck_number(String str_num, int size)
{
if (size <= 0)
{
//When given string number is empty
return;
}
int counter = 0;
//This is used to check valid positive number
boolean wrong = false;
boolean status = false;
// Skip the all beginning zeros
while (counter < size && str_num.charAt(counter) == '0')
{
counter++;
}
while (counter < size && wrong == false)
{
if (str_num.charAt(counter) >= '0' && str_num.charAt(counter) <= '9')
{
//Terminate loop when given number always valid number
if (str_num.charAt(counter) == '0')
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter++;
}
if (wrong == false && status == true)
{
System.out.print("\n [" + str_num + "] is duck number");
}
else
{
System.out.print("\n [" + str_num + "] is not duck number");
}
}
public static void main(String[] args)
{
DuckNumber obj = new DuckNumber();
//Test string numbers
String str_num1 = "529043043";
String str_num2 = "0001234";
String str_num3 = "00012304";
String str_num4 = "1000000";
String str_num5 = "-1000000";
String str_num6 = "2300200100";
int size = 0;
//Test case 1
size = str_num1.length();
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.length();
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.length();
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.length();
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.length();
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.length();
obj.is_duck_number(str_num6, size);
}
}``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````//Include header file
#include <iostream>
#include<string.h>

using namespace std;
/*
C++ program
Check Whether a number is duck number or not
*/
class DuckNumber
{
public:
// Determine whether given number is duck number or not
void is_duck_number(string str_num, int size)
{
if (size <= 0)
{
//When given string number is empty
return;
}
int counter = 0;
//This is used to check valid positive number
bool wrong = false;
bool status = false;
// Skip the all beginning zeros
while (counter < size && str_num[counter] == '0')
{
counter++;
}
while (counter < size && wrong == false)
{
if (str_num[counter] >= '0' && str_num[counter] <= '9')
{
//Terminate loop when given number always valid number
if (str_num[counter] == '0')
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter++;
}
if (wrong == false && status == true)
{
cout << "\n [" << str_num << "] is duck number";
}
else
{
cout << "\n [" << str_num << "] is not duck number";
}
}
};
int main()
{
DuckNumber obj = DuckNumber();
//Test string numbers
string str_num1 = "529043043";
string str_num2 = "0001234";
string str_num3 = "00012304";
string str_num4 = "1000000";
string str_num5 = "-1000000";
string str_num6 = "2300200100";
int size = 0;
//Test case 1
size = str_num1.size();
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.size();
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.size();
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.size();
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.size();
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.size();
obj.is_duck_number(str_num6, size);
return 0;
}``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````//Include namespace system
using System;

/*
C# program
Check Whether a number is duck number or not
*/

class DuckNumber
{
// Determine whether given number is duck number or not
public void is_duck_number(String str_num, int size)
{
if (size <= 0)
{
//When given string number is empty
return;
}
int counter = 0;
//This is used to check valid positive number
Boolean wrong = false;
Boolean status = false;
// Skip the all beginning zeros
while (counter < size && str_num[counter] == '0')
{
counter++;
}
while (counter < size && wrong == false)
{
if (str_num[counter] >= '0' && str_num[counter] <= '9')
{
//Terminate loop when given number always valid number
if (str_num[counter] == '0')
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter++;
}
if (wrong == false && status == true)
{
Console.Write("\n [" + str_num + "] is duck number");
}
else
{
Console.Write("\n [" + str_num + "] is not duck number");
}
}
public static void Main(String[] args)
{
DuckNumber obj = new DuckNumber();
//Test string numbers
String str_num1 = "529043043";
String str_num2 = "0001234";
String str_num3 = "00012304";
String str_num4 = "1000000";
String str_num5 = "-1000000";
String str_num6 = "2300200100";
int size = 0;
//Test case 1
size = str_num1.Length;
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.Length;
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.Length;
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.Length;
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.Length;
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.Length;
obj.is_duck_number(str_num6, size);
}
}``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````<?php
/*
Php program
Check Whether a number is duck number or not
*/
class DuckNumber
{
// Determine whether given number is duck number or not
public	function is_duck_number(\$str_num, \$size)
{
if (\$size <= 0)
{
//When given string number is empty
return;
}
\$counter = 0;
//This is used to check valid positive number
\$wrong = false;
\$status = false;
// Skip the all beginning zeros
while (\$counter < \$size && \$str_num[\$counter] == '0')
{
\$counter++;
}
while (\$counter < \$size && \$wrong == false)
{
if (ord(\$str_num[\$counter]) >= ord('0') && ord(\$str_num[\$counter]) <= ord('9'))
{
//Terminate loop when given number always valid number
if (\$str_num[\$counter] == '0')
{
//We get zero
\$status = true;
}
}
else
{
//When not a valid digit
\$wrong = true;
}
\$counter++;
}
if (\$wrong == false && \$status == true)
{
echo "\n [". \$str_num ."] is duck number";
}
else
{
echo "\n [". \$str_num ."] is not duck number";
}
}
}

function main()
{
\$obj = new DuckNumber();
//Test string numbers
\$str_num1 = "529043043";
\$str_num2 = "0001234";
\$str_num3 = "00012304";
\$str_num4 = "1000000";
\$str_num5 = "-1000000";
\$str_num6 = "2300200100";
\$size = 0;
//Test case 1
\$size = strlen(\$str_num1);
\$obj->is_duck_number(\$str_num1, \$size);
//Test case 2
\$size = strlen(\$str_num2);
\$obj->is_duck_number(\$str_num2, \$size);
//Test case 3
\$size = strlen(\$str_num3);
\$obj->is_duck_number(\$str_num3, \$size);
//Test case 4
\$size = strlen(\$str_num4);
\$obj->is_duck_number(\$str_num4, \$size);
//Test case 5
\$size = strlen(\$str_num5);
\$obj->is_duck_number(\$str_num5, \$size);
//Test case 6
\$size = strlen(\$str_num6);
\$obj->is_duck_number(\$str_num6, \$size);
}
main();``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````/*
Node Js program
Check Whether a number is duck number or not
*/
class DuckNumber
{
// Determine whether given number is duck number or not
is_duck_number(str_num, size)
{
if (size <= 0)
{
//When given string number is empty
return;
}
var counter = 0;
//This is used to check valid positive number
var wrong = false;
var status = false;
// Skip the all beginning zeros
while (counter < size && str_num[counter] == '0')
{
counter++;
}
while (counter < size && wrong == false)
{
if ((str_num[counter]).charCodeAt(0) >= ('0').charCodeAt(0) && (str_num[counter]).charCodeAt(0) <= ('9').charCodeAt(0))
{
//Terminate loop when given number always valid number
if (str_num[counter] == '0')
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter++;
}
if (wrong == false && status == true)
{
process.stdout.write("\n [" + str_num + "] is duck number");
}
else
{
process.stdout.write("\n [" + str_num + "] is not duck number");
}
}
}

function main()
{
var obj = new DuckNumber();
//Test string numbers
var str_num1 = "529043043";
var str_num2 = "0001234";
var str_num3 = "00012304";
var str_num4 = "1000000";
var str_num5 = "-1000000";
var str_num6 = "2300200100";
var size = 0;
//Test case 1
size = str_num1.length;
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.length;
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.length;
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.length;
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.length;
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.length;
obj.is_duck_number(str_num6, size);
}
main();``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````#   Python 3 program
#   Check Whether a number is duck number or not

class DuckNumber :
#  Determine whether given number is duck number or not
def is_duck_number(self, str_num, size) :
if (size <= 0) :
# When given string number is empty
return

counter = 0
# This is used to check valid positive number
wrong = False
status = False
#  Skip the all beginning zeros
while (counter < size and str_num[counter] == '0') :
counter += 1

while (counter < size and wrong == False) :
if (ord(str_num[counter]) >= ord('0') and ord(str_num[counter]) <= ord('9')) :
# Terminate loop when given number always valid number
if (str_num[counter] == '0') :
# We get zero
status = True

else :
# When not a valid digit
wrong = True

counter += 1

if (wrong == False and status == True) :
print("\n [", str_num ,"] is duck number", end = "")
else :
print("\n [", str_num ,"] is not duck number", end = "")

def main() :
obj = DuckNumber()
# Test string numbers
str_num1 = "529043043"
str_num2 = "0001234"
str_num3 = "00012304"
str_num4 = "1000000"
str_num5 = "-1000000"
str_num6 = "2300200100"
size = 0
# Test case 1
size = len(str_num1)
obj.is_duck_number(str_num1, size)
# Test case 2
size = len(str_num2)
obj.is_duck_number(str_num2, size)
# Test case 3
size = len(str_num3)
obj.is_duck_number(str_num3, size)
# Test case 4
size = len(str_num4)
obj.is_duck_number(str_num4, size)
# Test case 5
size = len(str_num5)
obj.is_duck_number(str_num5, size)
# Test case 6
size = len(str_num6)
obj.is_duck_number(str_num6, size)

if __name__ == "__main__": main()``````

#### Output

`````` [ 529043043 ] is duck number
[ 0001234 ] is not duck number
[ 00012304 ] is duck number
[ 1000000 ] is duck number
[ -1000000 ] is not duck number
[ 2300200100 ] is duck number``````
``````#   Ruby program
#   Check Whether a number is duck number or not

class DuckNumber
#  Determine whether given number is duck number or not
def is_duck_number(str_num, size)
if (size <= 0)
# When given string number is empty
return
end

counter = 0
# This is used to check valid positive number
wrong = false
status = false
#  Skip the all beginning zeros
while (counter < size && str_num[counter] == '0')
counter += 1
end

while (counter < size && wrong == false)
if ((str_num[counter]).ord >= ('0').ord && (str_num[counter]).ord <= ('9').ord)
# Terminate loop when given number always valid number
if (str_num[counter] == '0')
# We get zero
status = true
end

else
# When not a valid digit
wrong = true
end

counter += 1
end

if (wrong == false && status == true)
print("\n [", str_num ,"] is duck number")
else
print("\n [", str_num ,"] is not duck number")
end

end

end

def main()
obj = DuckNumber.new()
# Test string numbers
str_num1 = "529043043"
str_num2 = "0001234"
str_num3 = "00012304"
str_num4 = "1000000"
str_num5 = "-1000000"
str_num6 = "2300200100"
size = 0
# Test case 1
size = str_num1.length()
obj.is_duck_number(str_num1, size)
# Test case 2
size = str_num2.length()
obj.is_duck_number(str_num2, size)
# Test case 3
size = str_num3.length()
obj.is_duck_number(str_num3, size)
# Test case 4
size = str_num4.length()
obj.is_duck_number(str_num4, size)
# Test case 5
size = str_num5.length()
obj.is_duck_number(str_num5, size)
# Test case 6
size = str_num6.length()
obj.is_duck_number(str_num6, size)
end

main()``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````/*
Scala program
Check Whether a number is duck number or not
*/
class DuckNumber
{
// Determine whether given number is duck number or not
def is_duck_number(str_num: String, size: Int): Unit = {
if (size <= 0)
{
//When given string number is empty
return;
}
var counter: Int = 0;
//This is used to check valid positive number
var wrong: Boolean = false;
var status: Boolean = false;
// Skip the all beginning zeros
while (counter < size && str_num(counter) == '0')
{
counter += 1;
}
while (counter < size && wrong == false)
{
if (str_num(counter) >= '0' && str_num(counter) <= '9')
{
//Terminate loop when given number always valid number
if (str_num(counter) == '0')
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter += 1;
}
if (wrong == false && status == true)
{
print("\n [" + str_num + "] is duck number");
}
else
{
print("\n [" + str_num + "] is not duck number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: DuckNumber = new DuckNumber();
//Test string numbers
var str_num1: String = "529043043";
var str_num2: String = "0001234";
var str_num3: String = "00012304";
var str_num4: String = "1000000";
var str_num5: String = "-1000000";
var str_num6: String = "2300200100";
var size: Int = 0;
//Test case 1
size = str_num1.length();
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.length();
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.length();
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.length();
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.length();
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.length();
obj.is_duck_number(str_num6, size);
}
}``````

#### Output

`````` [529043043] is duck number
[0001234] is not duck number
[00012304] is duck number
[1000000] is duck number
[-1000000] is not duck number
[2300200100] is duck number``````
``````/*
Swift 4 program
Check Whether a number is duck number or not
*/
class DuckNumber
{
// Determine whether given number is duck number or not
func is_duck_number(_ text: String, _ size: Int)
{
if (size <= 0)
{
//When given string number is empty
return;
}
let str_num = Array(text);
var counter: Int = 0;
//This is used to check valid positive number
var wrong: Bool = false;
var status: Bool = false;
// Skip the all beginning zeros
while (counter < size && str_num[counter] == "0")
{
counter += 1;
}
while (counter < size && wrong == false)
{
if (str_num[counter] >= "0" && str_num[counter] <= "9")
{
//Terminate loop when given number always valid number
if (str_num[counter] == "0")
{
//We get zero
status = true;
}
}
else
{
//When not a valid digit
wrong = true;
}
counter += 1;
}
if (wrong == false && status == true)
{
print("\n [", text ,"] is duck number", terminator: "");
}
else
{
print("\n [", text ,"] is not duck number", terminator: "");
}
}
}
func main()
{
let obj: DuckNumber = DuckNumber();
//Test string numbers
let str_num1: String = "529043043";
let str_num2: String = "0001234";
let str_num3: String = "00012304";
let str_num4: String = "1000000";
let str_num5: String = "-1000000";
let str_num6: String = "2300200100";
var size: Int = 0;
//Test case 1
size = str_num1.count;
obj.is_duck_number(str_num1, size);
//Test case 2
size = str_num2.count;
obj.is_duck_number(str_num2, size);
//Test case 3
size = str_num3.count;
obj.is_duck_number(str_num3, size);
//Test case 4
size = str_num4.count;
obj.is_duck_number(str_num4, size);
//Test case 5
size = str_num5.count;
obj.is_duck_number(str_num5, size);
//Test case 6
size = str_num6.count;
obj.is_duck_number(str_num6, size);
}
main();``````

#### Output

`````` [ 529043043 ] is duck number
[ 0001234 ] is not duck number
[ 00012304 ] is duck number
[ 1000000 ] is duck number
[ -1000000 ] is not duck number
[ 2300200100 ] is duck number``````

## Time Complexity

Let's analyze the time complexity of the given algorithm. In the worst case, we need to iterate through the entire string to check if all digits are valid and if there is at least one zero. Therefore, the time complexity is O(n), where n is the number of digits in the input string.

## Resultant Output Explanation

The provided program uses the `is_duck_number` function to check whether each given number is a Duck Number or not. The program prints the result for each test case.

• For the input "529043043", the output is "[529043043] is duck number" because the number contains a zero and all other digits are non-zero.

• For the input "0001234", the output is "[0001234] is not duck number" as there are leading zeros and not all other digits are non-zero.

• For the input "00012304", the output is "[00012304] is duck number" as there is a zero and all other digits are non-zero.

• For the input "1000000", the output is "[1000000] is duck number" as there is a zero and all other digits are non-zero.

• For the input "-1000000", the output is "[-1000000] is not duck number" as the number is not positive.

• For the input "2300200100", the output is "[2300200100] is duck number" as there is a zero and all other digits are non-zero.

The program correctly identifies the Duck Numbers among the given test cases and outputs the results accordingly.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post