Duck Number Java

Duck number is another special positive non-zero number that contains zero in it. The digit zero should not be presented at the starting of the number. Zero can be present at any of the positions except the beginning of the number.

Let’s understand some examples of Duck numbers.

  1. 3210 is a Duck number because it contains zero at the end of the number but not present at the beginning of it.
  2. 08237 is not a Duck number because it contains zero at the beginning of it.
  3. 7033 is a Duck number because it contains zero at the second position, not at the beginning.
  4. 030405 is not a Duck number because it also contains zero in starting of the number.
  5. 00153 is also not a Duck number because it contains leading zeros.

These are the following steps that we use to check whether the given number is a Duck number or not.

  1. We first take a number.
  2. We then find the last digit of the number.
  3. If the last digit is zero, it is a Duck number.
  4. Otherwise, we remove that digit from the number.
  5. Perform steps 2, 3, and 4 until the number becomes zero.

Let’s implement the code to check a valid Duck number.

DuckNumber.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create DuckNumberExample class to check whether the given number is a Duck number or not  
  7. public class DuckNumberExample {  
  8.   
  9.    // create checkNumber() method that returns true when it founds number Buzz   
  10.    public static boolean checkNumber(int number) {  
  11.   
  12.       // use loop to repeat steps  
  13.       while(number != 0) {  
  14.   
  15.          // check whether the last digit of the number is zero or not  
  16.          if(number%10 == 0)  
  17.             return true;    //return true if the number is Duck  
  18.   
  19.          // divide the number by 10 to remove the last digit  
  20.          number = number / 10;  
  21.       }  
  22.   
  23.       return false; //return false if the number is not Duck  
  24.    }  
  25.        // main() method start  
  26.     public static void main(String args[])   
  27.     {     
  28.         int n1, n2;  
  29.           
  30.         //create scanner class object to get input from user  
  31.         Scanner sc=new Scanner(System.in);  
  32.           
  33.         //show custom message  
  34.         System.out.println(“Enter first number”);  
  35.           
  36.         //store user entered value into variable n1  
  37.         n1 = sc.nextInt();  
  38.           
  39.         //show custom message  
  40.         System.out.println(“Enter second number”);  
  41.           
  42.         //store user entered value into variable n2  
  43.         n2 = sc.nextInt();  
  44.           
  45.         if (checkNumber(n1))   
  46.             System.out.println(n1 + ” is a Duck number”);   
  47.         else  
  48.             System.out.println(n1 + ” is not a Duck number”);   
  49.         if (checkNumber(n2))   
  50.             System.out.println(n2 + ” is a Duck number”);   
  51.         else  
  52.             System.out.println(n2 + ” is not a Duck number”);   
  53.     }  
  54. }  

Output

Duck Number Java

Let’s implement one more program to get all the Duck number in a given range

FindAllDuckNumber.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create FindAllDuckNumber class to get all the Duck number in a given range  
  7. class FindAllDuckNumber  
  8. {  
  9.     //main() method start  
  10.     public static void main(String args[])  
  11.     {  
  12.         int range;  
  13.           
  14.         //create scanner class object  
  15.         Scanner sc=new Scanner(System.in);  
  16.           
  17.         //show custom message  
  18.         System.out.println(“Enter the value of range”);  
  19.           
  20.         //store user entered value into variable range  
  21.         range = sc.nextInt();  
  22.   
  23.         for(int i = 1; i <= range; i++)  
  24.             if(checkNumber(i)){  
  25.                 System.out.println(i + ” is a Duck number”);  
  26.             }  
  27.     }  
  28.   
  29.     // create checkNumber() method that returns true when it founds number Buzz   
  30.     public static boolean checkNumber(int number) {  
  31.   
  32.         // use loop to repeat steps  
  33.         while(number != 0) {  
  34.   
  35.             // check whether the last digit of the number is zero or not  
  36.             if(number%10 == 0)  
  37.                 return true;    //return true if the number is Duck  
  38.   
  39.             // divide the number by 10 to remove the last digit  
  40.             number = number / 10;  
  41.         }  
  42.   
  43.         return false;   //return false if the number is not Duck  
  44.    }  
  45. }  

Output

Duck Number Java

Leave a Comment