CodersZone logo

Duck Number

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.

Algorithm

  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.

Code

	//import required classes and packages  
	import java.util.*;   
	import java.io.*;   
	import java.util.Scanner;  
	  
	//create DuckNumberExample class to check whether the given number is a Duck number or not  
	public class DuckNumberExample {  
	  
	   // create checkNumber() method that returns true when it founds number Buzz   
	   public static boolean checkNumber(int number) {  
	  
	      // use loop to repeat steps  
	      while(number != 0) {  
	  
	         // check whether the last digit of the number is zero or not  
	         if(number%10 == 0)  
	            return true;    //return true if the number is Duck  
	  
	         // divide the number by 10 to remove the last digit  
	         number = number / 10;  
	      }  
	  
	      return false; //return false if the number is not Duck  
	   }  
	       // main() method start  
	    public static void main(String args[])   
	    {     
            int n1, n2;  
	          
	        //create scanner class object to get input from user  
	        Scanner sc=new Scanner(System.in);  
	          
	        //show custom message  
	        System.out.println("Enter first number");  
	          
	        //store user entered value into variable n1  
	        n1 = sc.nextInt();  
	          
	        //show custom message  
	        System.out.println("Enter second number");  
	          
	        //store user entered value into variable n2  
	        n2 = sc.nextInt();  
	          
	        if (checkNumber(n1))   
	            System.out.println(n1 + " is a Duck number");   
	        else  
	            System.out.println(n1 + " is not a Duck number");   
	        if (checkNumber(n2))   
	            System.out.println(n2 + " is a Duck number");   
	        else  
	            System.out.println(n2 + " is not a Duck number");   
	    }  
	}  

            

Output

Enter first number 
00432
Enter second number
0340240
432 is not a Duck number
340240 is a Duck number

 
See the same program in C See the same program in Python