Check Whether a number is Duck Number or not

A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example 3210, 8050896, 70709 are all duck numbers whereas 02364, 03401 are not.
The task is to check whether the given number is a duck number or not.

Examples :

Input : 707069
Output : It is a duck number.
Explanation: 707069 does not contains zeros at the beginning.

Input : 02364
Output : It is not a duck number.
Explanation: in 02364 there is a zero at the beginning of the number.

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to check whether 
// a number is Duck Number or not.
#include <iostream>
#include <string.h>
using namespace std;
  
// Function to check whether 
// given number is duck number or not.
int check_duck( char *num)
{
    // Length of the number(number of digits)
    int len = strlen(num); 
    int count_zero = 0 ; 
    char ch;
  
    for(int i = 1;i < len ;i++)
    {
        // Checking for a digit whether it is a '0' or not
        ch=num[i]; 
        if(ch=='0')
            count_zero++;
    }
  
    return count_zero ;
}
  
// Driver Method
int main(void)
{
    char num1[] = "1023"
  
    // Extracting the first digit
    char first_digit1 = num1[0]; 
  
    if( check_duck(num1) > 0 && first_digit1 != '0')
        cout << "It is a duck number\n";
    else
        cout <<"It is not a duck number\n";
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to check whether a 
// number is Duck Number or not.
  
import java.io.*;
class GFG
{
    // Function to check whether 
    // the given number is duck number or not.
    static int check_duck( String num)
    {
      // Length of the number(number of digits)
        int len = num.length(); 
        int count_zero = 0
        char ch;
   
        for(int i = 1;i < len ;i++)
        {
            // Checking for a digit whether it is a '0' or not
            ch=num.charAt(i); 
            if(ch=='0')
                count_zero++;
        }  
        return count_zero ;
    }
      
  
    // Driver Method
    public static void main(String args[])throws IOException
    {
        String num1 = "1023"
          
        // extracting the first digit
        char first_digit1 = num1.charAt(0); 
          
        // checking number1
        if( check_duck(num1) > 0 && first_digit1 != '0')
            System.out.println("It is a duck number");
        else
            System.out.println("It is not a duck number");
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check whether a number is Duck Number or not.
  
# Function to check whether 
# the given number is duck number or not.
def check_duck(num ) :
    # Length of the number(number of digits)
    l = len(num) 
    count_zero = 0 
    i = 1
    while i < l :
        # Checking for a digit whether it is a '0' or not
        ch = num[i]
        if(ch == "0") :
            count_zero = count_zero + 1
        i = i + 1
      
    return count_zero 
      
  
# Driver Method
num1 = "1023" 
  
# Extracting the first digit
first_digit1 = num1[0]
  
# Checking number1
if( check_duck(num1) > 0 and first_digit1 != '0') :
    print  "It is a duck number"
else :
    print "It is not a duck number"

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to check whether a 
// number is Duck Number or not.
using System;
  
class GFG {
      
    // Function to check whether 
    // the given number is duck
    // number or not.
    static int check_duck( String num)
    {
          
        // Length of the number(number
        // of digits)
        int len = num.Length; 
        int count_zero = 0 ; 
        char ch;
  
        for(int i = 1; i < len; i++)
        {
              
            // Checking for a digit
            // whether it is a '0'
            // or not
            ch = num[i]; 
              
            if(ch == '0')
                count_zero++;
        
          
        return count_zero ;
    }
      
  
    // Driver Method
    public static void Main()
    {
          
        String num1 = "1023"
          
        // extracting the first digit
        char first_digit1 = num1[0]; 
          
        // checking number1
        if( check_duck(num1) > 0 && 
                  first_digit1 != '0')
            Console.Write("It is a "
                      + "duck number");
        else
            Console.Write("It is not "
                    + "a duck number");
    }
}
  
// This code is contributed by
// nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to check whether 
// a number is Duck Number or not.
  
// Function to check whether 
// given number is duck number or not.
function check_duck( $num)
{
    // Length of the number
    // (number of digits)
    $len = strlen($num); 
    $count_zero = 0 ; 
      
    for($i = 1; $i < $len ;$i++)
    {
        // Checking for a digit 
        // whether it is a '0' or not
        $ch = $num[$i]; 
        if($ch == '0')
            $count_zero++;
    }
  
    return $count_zero ;
}
  
// Driver Code
$num1 = "1023"
  
// Extracting the first digit
$first_digit1 = $num1[0]; 
  
if( check_duck($num1) > 0 && 
    $first_digit1 != '0')
    echo "It is a duck number\n";
else
    echo"It is not a duck number\n";
  
// This code is contributed by Sam007
?>

chevron_right



Output :



It is a duck number.

This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, Sam007