Duck Number in Java

A duck number is a positive integer that has one or more zeroes present in it. However, the number should not begin with zero.

For instance, 1205, 500, etc. are duck numbers.

Write a program in Java to accept a positive integer from the user, and check whether it is a duck number.

import java.io.*;
class Duck{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Positive integer: ");
        int num = Math.abs(Integer.parseInt(br.readLine()));
        int count = 0;
        for(int i = num; i > 0; i /= 10){
            int digit = i % 10;
            if(digit == 0)
                count++;
        }
        if(count > 0)
            System.out.println(num + " is a duck number.");
        else
            System.out.println(num + " is not a duck number.");
    }
}

Leave a Reply

%d bloggers like this: