Explaining Duck Number in C#

In this article, I am going to discuss the Duck Number Program in C# with examples. This detailed article will cover the following topics

  1. Introduction
  2. What is Duck Number?
  3. Algorithm to check Duck Number
  4. Program to check whether a given number is a Duck Number or not in C#
  5. Program to print Duck Numbers between 1 to 150
  6. Conclusion

What is Duck Number?

A "Duck Number" is a positive number that contains zeroes, however the zero shouldn't be at the beginning of the number. It may have more than one zero(s). In other words, a duck number is a number that has at least one zero, but does not start with a zero.

  • The number must contain at least one '0'.
  • The number should not start with '0'.

Examples

  • Input: 2046 -> Duck Number
  • Explanation: Number contains zero(s).
  • Input: 2100 -> Duck Number
  • Explanation: Number contains zero(s).
  • Input: 0123 -> Not a Duck Number
  • Explanation: It starts from zero.
  • Input: 0120 -> Duck Number
  • Explanation: Number contains zero(s).
  • Input: 001200 -> Duck Number
  • Explanation: Number contains zero(s).

Algorithm to check Duck Number

To check whether the given number is a Duck number or not, below are the steps.

  1. Get the input from the user that you want to check if it is a Duck Number.
  2. Converts it to a string for easier manipulation.
  3. Check,
    • If '0' appears anywhere in the string.
    • If the first digit of the string is '0', it is not a valid Duck Number.
  4. If the condition is true, then it will print a Duck Number
  5. If the condition is not true, then print that it is not a Duck Number.

Program to check whether a given number is a Duck Number or not in C#

The following C# program will enable the user to enter a number and check whether the number is a Duck number or not. Let's see.

internal class DuckNumberInCsharp
{
    public static void Main(string[] args)
    {
        try
        {
            Console.Write("Enter a number: ");
            var number = Convert.ToInt32(Console.ReadLine());

            if (IsDuckNumber(number))
            {
                Console.WriteLine("The given number is a Duck Number!");
            }
            else
            {
                Console.WriteLine("The given number is not a Duck Number!");
            }

            Console.ReadLine();
        }
        catch (FormatException)
        {
            Console.WriteLine("Oopss, invalid input! Please enter a valid integer");
        }
    }

    public static bool IsDuckNumber(int num)
    {
        string strNum = num.ToString();

        if (strNum[0] == '0')
        {
            return false;
        }

        return strNum.Contains('0');
    }
}

Output

Program to print Duck Numbers between 1 to 150

The following C# program will enable the user to print Duck Numbers between 1 to 150. Let's see.

internal class DuckNumberBetween1to100
{
    public static void Main()
    {
        Console.WriteLine("!! To Print Duck Numbers Between 1 to 150 !!");
        Console.Write("Duck Numbers: ");

        for (int num = 1; num < 150; num++)
        {
            string numberStr = num.ToString();

            // Check if number contains '0' and doesn't start with '0'
            if (numberStr.Contains('0') && numberStr[0] != '0')
            {
                Console.Write(num + ", ");
            }
        }
        Console.ReadLine();
    }
}

Output

See you in the next article, till then, take care and be happy learning.

You may also visit my other article,

You can connect with me @

Conclusion

In this article, we have discussed how to check whether a given number is a Duck Number or not in C# with examples.

I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about C#.

Thanks for reading.


Similar Articles