Posted on by Kalkicode
Code Number

Duck Number

A Duck Number is a number that contains at least one zero in it, but it's not leading zero. For example, 102 is a duck number, but 023 is not because it has a leading zero. The name "duck number" comes from the fact that the appearance of the number resembles a duck, as the zero resembles the duck's beak. Duck numbers are used in various mathematical puzzles and games.

Here given code implementation process.

/*
  C Program 
+ Check if a given number is Duck Number or not
*/
#include<stdio.h>

//Check that given number is duck number or not
void is_duck_number(char str_num[],int size)
{
  int status = 0;
  //When Numbers start with zero not a duck number
  if(str_num[0]!='0')
  {
    //When not start to 0

    for (int i = 1; i < size && status == 0; ++i)
    {
      if(str_num[i]=='0')
      { //When get the intermediate zero
        status=1;
      }
    }
  }
  if(status==0)
  {
    printf("%s Is not an Duck Number \n",str_num );
  }
  else
  {
    printf("%s Is an Duck Number \n",str_num );
  }
}
int main(){
  
  int size = 0;
  //Test Cases
  char str_num1[]="16460";
  char str_num2[]="026460";
  char str_num3[]="264665";

  //Get the size
  size= sizeof(str_num1)/sizeof(str_num1[0])-1;
  is_duck_number(str_num1,size);

  size= sizeof(str_num2)/sizeof(str_num2[0])-1;
  is_duck_number(str_num2,size);

  size= sizeof(str_num3)/sizeof(str_num3[0])-1;
  is_duck_number(str_num3,size);
  return 0;
}

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
/*
 C++ Program
 Check if a given number is Duck Number or not
*/
#include<iostream>
#include <string.h>
using namespace std;

class MyNumber {
    public:

        //Check that given number is duck number or not
        void is_duck_number(string str_num) {
            //get length
            int size = str_num.length();
            bool status = false;
            //When Numbers start with zero not a duck number

            if (str_num[0] != '0') {
                //When not start to 0

                for (int i = 1; i < size && status == false; ++i) {
                    if (str_num[i] == '0') {
                        //When get the intermediate zero
                        status = true;
                    }
                }
            }
            if (status == false) {
                cout << str_num << " Is not an Duck Number \n";
            } else {
                cout << str_num << " Is an Duck Number \n";
            }
        }
};
int main() {
    MyNumber obj;
    //Assume Given input string
    string str_num1 = "16460";
    string str_num2 = "026460";
    string str_num3 = "264665";
    //Test Cases
    obj.is_duck_number(str_num1);
    obj.is_duck_number(str_num2);
    obj.is_duck_number(str_num3);
    return 0;
}

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
/*
  Java Program
  Check if a given number is Duck Number or not
*/
public class MyNumber {
 
 
  //Check that given number is duck number or not
  public void is_duck_number(String str_num)
  {
    //get length
    int size = str_num.length();

    Boolean status = false;
    
    //When Numbers start with zero not a duck number
    if(str_num.charAt(0)!='0')
    {
      //When not start to 0

      for (int i = 1; i < size && status == false; ++i)
      {
        if(str_num.charAt(i)=='0')
        { //When get the intermediate zero
          status=true;
        }
      }
    }
    if(status==false)
    {
      System.out.print(str_num+" Is not an Duck Number \n" );
    }
    else
    {
      System.out.print(str_num+" Is an Duck Number \n" );
    }
  }
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Assume Given input string
    String str_num1="16460";
    String str_num2="026460";
    String str_num3="264665";

    //Test Cases
    obj.is_duck_number(str_num1);

    obj.is_duck_number(str_num2);

    obj.is_duck_number(str_num3);


  }
}

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
/*
  C# Program
  Check if a given number is Duck Number or not
*/
using System;
public class MyNumber {


    //Check that given number is duck number or not
    public void is_duck_number(String str_num) {
        //get.Length
        int size = str_num.Length;

        Boolean status = false;

        //When Numbers start with zero not a duck number
        if (str_num[0] != '0') {
            //When not start to 0

            for (int i = 1; i < size && status == false; ++i) {
                if (str_num[i] == '0') { 
                  //When get the intermediate zero
                    status = true;
                }
            }
        }
        if (status == false) {
            Console.Write(str_num + " Is not an Duck Number \n");
        } else {
            Console.Write(str_num + " Is an Duck Number \n");
        }
    }
    public static void Main(String[] args) {

        MyNumber obj = new MyNumber();

        //Assume Given input string
        String str_num1 = "16460";
        String str_num2 = "026460";
        String str_num3 = "264665";

        //Test Cases
        obj.is_duck_number(str_num1);

        obj.is_duck_number(str_num2);

        obj.is_duck_number(str_num3);


    }
}

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
<?php
/*
  Php Program
  Check if a given number is Duck Number or not
*/
class MyNumber {
    //Check that given number is duck number or not

    public  function is_duck_number($str_num) {
        //get length
        $size = strlen($str_num);
        $status = false;
        //When Numbers start with zero not a duck number

        if ($str_num[0] != '0') {
            //When not start to 0

            for ($i = 1; $i < $size && $status == false; ++$i) {
                if ($str_num[$i] == '0') {
                    //When get the intermediate zero
                    $status = true;
                }
            }
        }
        if ($status == false) {
            echo($str_num ." Is not an Duck Number \n");
        } else {
            echo($str_num ." Is an Duck Number \n");
        }
    }
};

function main() {
    $obj = new MyNumber();
    //Assume Given input string
    $str_num1 = "16460";
    $str_num2 = "026460";
    $str_num3 = "264665";
    //Test Cases

    $obj->is_duck_number($str_num1);
    $obj->is_duck_number($str_num2);
    $obj->is_duck_number($str_num3);
}
main();

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
/*
 Node Js Program
 Check if a given number is Duck Number or not
*/
class MyNumber {
    //Check that given number is duck number or not
    is_duck_number(str_num) {
        //get length
        var size = str_num.length;
        var status = false;
        //When Numbers start with zero not a duck number

        if (str_num[0] != '0') {
            //When not start to 0

            for (var i = 1; i < size && status == false; ++i) {
                if (str_num[i] == '0') {
                    //When get the intermediate zero
                    status = true;
                }
            }
        }
        if (status == false) {
            process.stdout.write(str_num + " Is not an Duck Number \n");
        } else {
            process.stdout.write(str_num + " Is an Duck Number \n");
        }
    }
}

function main(args) {
    var obj = new MyNumber();
    //Assume Given input string
    var str_num1 = "16460";
    var str_num2 = "026460";
    var str_num3 = "264665";
    //Test Cases
    obj.is_duck_number(str_num1);
    obj.is_duck_number(str_num2);
    obj.is_duck_number(str_num3);
}
main();

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
# Python 3 Program
# Check if a given number is Duck Number or not
class MyNumber :
    # Check that given number is duck number or not
    def is_duck_number(self, str_num) :
        # get length
        size = len(str_num)
        status = False
        # When Numbers start with zero not a duck number

        if (str_num[0] != '0') :
            # When not start to 0
            i = 1
            while (i < size and status == False) :
                if (str_num[i] == '0') :
                    # When get the intermediate zero
                    status = True
                
                i += 1
            
        
        if (status == False) :
            print(str_num ," Is not an Duck Number \n")
        else :
            print(str_num ," Is an Duck Number \n")
        
    

def main() :
    obj = MyNumber()
    # Assume Given input string
    str_num1 = "16460"
    str_num2 = "026460"
    str_num3 = "264665"
    # Test Cases
    obj.is_duck_number(str_num1)
    obj.is_duck_number(str_num2)
    obj.is_duck_number(str_num3)


if __name__ == "__main__":
    main()

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
# Ruby Program 
# Check if a given number is Duck Number or not
class MyNumber 
    # Check that given number is duck number or not
    def is_duck_number(str_num) 
        # get length
        size = str_num.length
        status = false
        # When Numbers start with zero not a duck number

        if (str_num[0] != '0') 
            # When not start to 0
            i = 1
            while (i < size and status == false) 
                if (str_num[i] == '0') 
                    # When get the intermediate zero
                    status = true
                end
                i += 1
            end
        end
        if (status == false) 
            print(str_num ," Is not an Duck Number \n")
        else 
            print(str_num ," Is an Duck Number \n")
        end
    end
end
def main() 
    obj = MyNumber.new()
    # Assume Given input string
    str_num1 = "16460"
    str_num2 = "026460"
    str_num3 = "264665"
    # Test Cases
    obj.is_duck_number(str_num1)
    obj.is_duck_number(str_num2)
    obj.is_duck_number(str_num3)
end
main()

Output

16460 Is an Duck Number 
026460 Is not an Duck Number 
264665 Is not an Duck Number 
/*
 Scala Program
 Check if a given number is Duck Number or not
*/
class MyNumber {
    //Check that given number is duck number or not
    def is_duck_number(str_num: String): Unit = {
        //get length
        var size: Int = str_num.length();
        var status: Boolean = false;
        //When Numbers start with zero not a duck number

        if (str_num(0) != '0') {
            //When not start to 0
            var i: Int = 1;
            while (i < size && status == false) {
                if (str_num(i) == '0') {
                    //When get the intermediate zero
                    status = true;
                }
                i += 1;
            }
        }
        if (status == false) {
            print(str_num + " Is not an Duck Number \n");
        } else {
            print(str_num + " Is an Duck Number \n");
        }
    }
}
object Main {
    def main(args: Array[String]): Unit = {
        var obj: MyNumber = new MyNumber();
        //Assume Given input string
        var str_num1: String = "16460";
        var str_num2: String = "026460";
        var str_num3: String = "264665";
        //Test Cases
        obj.is_duck_number(str_num1);obj.is_duck_number(str_num2);obj.is_duck_number(str_num3);
    }
}

Output

16460 Is an Duck Number
026460 Is not an Duck Number
264665 Is not an Duck Number
/*
  Swift 4 Program
  Check if a given number is Duck Number or not
*/
class MyNumber {
    //Check that given number is duck number or not
    func is_duck_number(_ str_num: String) {
    
        var status: Bool = false;
        //When Numbers start with zero not a duck number
        var index = str_num.startIndex
        if (str_num[index] != "0") {
            //When not start to 0

            index = str_num.index(after: index)
            while index != str_num.endIndex && status == false{
              // Check index value     
              if(str_num[index]=="0")
              { 
                status = true
              }
              index = str_num.index(after: index)
            }
        }
        if (status == false) {
            print(str_num ," Is not an Duck Number ");
        } else {
            print(str_num ," Is an Duck Number ");
        }
    }
}
func main() {
    let obj: MyNumber = MyNumber();
    //Assume Given input string
    let str_num1: String = "16460";
    let str_num2: String = "026460";
    let str_num3: String = "264665";
    //Test Cases
    obj.is_duck_number(str_num1);
    obj.is_duck_number(str_num2);
    obj.is_duck_number(str_num3);
}
main();

Output

16460  Is an Duck Number
026460  Is not an Duck Number
264665  Is not an Duck Number

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.

New Comment