CodersZone logo

Duck Number

Duck number is another special positive non-zero number that contains zero in it. The digit zero should not be presented at the starting of the number. Zero can be present at any of the positions except the beginning of the number.

Let's understand some examples of Duck numbers.

1. 3210 is a Duck number because it contains zero at the end of the number but not present at the beginning of it.
2. 08237 is not a Duck number because it contains zero at the beginning of it.
3. 7033 is a Duck number because it contains zero at the second position, not at the beginning.
4. 030405 is not a Duck number because it also contains zero in starting of the number.
5. 00153 is also not a Duck number because it contains leading zeros.

Algorithm

  1. We first take a number
  2. We then find the last digit of the number.
  3. If the last digit is zero, it is a Duck number.
  4. Otherwise, we remove that digit from the number.
  5. Perform steps 2, 3, and 4 until the number becomes zero.

Code

    # include < stdio.h>
    # include < stdlib.h>
                                  
    int main(){
        int dno, dkno, r, flg;
        flg=0;

        printf("\n\n Check whether a number is a Duck Number or not: \n");
        printf(" ----------------------------------------------------\n");
        printf(" Input a number: ");
        scanf("%d",&dkno);
        dno=dkno;
                        
        while(dkno>0){
            if(dkno % 10 == 0){
                flg=1;
                break;
            }
                                
            dkno/=10;
        }

        if(dno>0 && flg==1){
            printf(" The given number is a Duck Number.\n");
        }

        else{
            printf(" The given number is not a Duck Number.\n");
        }
    }
                    
            

Output

    Check whether a number is a Duck Number or not: 
    ----------------------------------------------------
    Input a number: 3210
    The given number is a Duck Number.
 
See the same program in Java See the same program in Python