Java Program to check duck number

Java Program to check duck number

In any number if there is one or more zero then that number is a duck number. But if the zero is in first digit of the number then that number is not a duck number. For example 105, 20, 3030, 701.

But 020, 0100, 0104 are not duck number as these numbers start with zero.

import java.io.*;
class DuckNo
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any number : ");
        String n=br.readLine(); //inputting the number and storing it in a String
        int l=n.length(); //finding the length (number of digit) of the number
        int c=0; //variable for counting number of zero digits
        char ch;
        for(int i=1;i<l;i++)
        {
            //extracting each digit and checking whether it is a '0' or not
            ch=n.charAt(i);
            if(ch=='0')
                c++;
        }
        char f=n.charAt(0); //taking out the first digit of the in putted number
        if(c>0 && f!='0')
            System.out.println("It is a duck number");
        else
            System.out.println("It is not a duck number");
    }
}

Output
Enter any number : 105
It is a duck number

Enter any number : 020
It is not a duck number

Enter any number : 10006
It is a duck number


You may also be interested in these programs
Peterson Number, Magic Number, Krishnamurty Number


SHARE THIS
Previous Post
Next Post