Monday , 25 July 2016
Home » Class 10 » Program to check for Duck Number

Program to check for Duck Number

Question:

Write a Program in Java to input a number and check whether it is a Duck Number or not.

Note: A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not.

Solution:

/**
* The class Duck_No inputs a number and checks whether it is a Duck Number or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Duck_No
{
    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++)
        {
            ch=n.charAt(i); //extracting each digit and checking whether it is a '0' or not
            if(ch=='0')
                c++;
        }

        char f=n.charAt(0); //taking out the first digit of the inputted 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 : 4013
It is a duck number

Enter any number : 0134
It is not a duck number

Enter any number : 67300987
It is a duck number

[Note: Don’t forget to share these resources and links from our website in your social networking sites with your friends and followers.]

Check Also

default thumbnail

Java Program to Sort an Array using Bubble Sort Algorithm

Java program to input an array of integers and sort it using Bubble Sorting Algorithm.

6 comments

  1. Can you give me the solution for the special two digit no.
    Example:
    the no. is 59
    The sum of digits is:5+9=14
    The product of the digits is:5*9=45
    The sum of the sum and product=14+45=59

  2. Programs are nice and easy to understand….

  3. Very easy and good program for students.

  4. sir/madam, can we make this program like this
    public class vv
    {
    public static void main(String args[])
    {

    String n=”562045″; //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=2;i0)
    System.out.println(“It is a duck number”);
    else
    System.out.println(“It is not a duck number”);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *