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++ 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;
}

Output:

It is a duck number.

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org/check-whether-number-duck-number-not/
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar