w3resource

C Exercises: Check whether a number is a Duck Number or not


23. Duck Number Check Variants

Write a program in C to check whether a number is a Duck Number or not.

Test Data
Input a number: 3210

Sample Solution:

C Code:

# include <stdio.h>
# include <stdlib.h>

int main()
{
    int dno, dkno, r, flg; // Variable declaration: dno stores the original number, dkno stores the number for manipulation, r stores the remainder, flg is a flag for checking

    flg = 0; // Initializing flag to 0

    // Printing information about the program and asking for user input
    printf("\n\n Check whether a number is a Duck Number or not: \n");
    printf(" ----------------------------------------------------\n");
    printf(" Input a number: ");
    scanf("%d", &dkno); // Reading input from the user
    dno = dkno; // Storing the original number

    while (dkno > 0) // Loop to check if the number is a Duck Number
    {
        if (dkno % 10 == 0) // Checking if any digit of the number is zero
        {
            flg = 1; // Setting flag to 1 if zero is found
            break; // Exiting the loop if zero is found
        }
        dkno /= 10; // Moving to the next digit in the number
    }

    if (dno > 0 && flg == 1) // Checking if the original number is positive and a zero was found
    {
        printf(" The given number is a Duck Number.\n"); // Printing if the number is a Duck Number
    }
    else
    {
        printf(" The given number is not a Duck Number.\n"); // Printing if the number is not a Duck Number
    }

    return 0; // Returning 0 to indicate successful execution
}

Sample Output:

 Input a number: 3210                                                                                         
 The given number is a Duck Number. 

Visual Presentation:

C programming: Check whether a number is a Duck Number or not.

Flowchart:

Flowchart: Check whether a number is a Duck Number or not

For more Practice: Solve these Related Problems:

  • Write a C program to determine if a number is a duck number by checking for embedded zeros.
  • Write a C program to verify duck numbers using string manipulation to ignore leading zeros.
  • Write a C program to list duck numbers and ensure numbers with leading zeros are excluded.
  • Write a C program to check duck number property across different numerical bases.

Go to:


PREV : Automorphic Numbers Between 1 and 1000 Variants.
NEXT : Duck Numbers Between 1 and 500 Variants.

C Programming Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.