Duck Number In Java

In this tutorial we will see what is a duck number , How to check given number is duck number in java or not and print all duck number in a given range using java program .

What is a Duck?

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. In other words , A duck number is a positive non-zero number which has zero present in it. The zero is present at any position of that number except at the starting of the number .

Example of duck number :

  1. 2210 is a Duck number because it contains zero , but not at the beginning .
  2. 0253  is not a Duck number because it contains zero at the beginning of number.
  3. 030201 is not a Duck number because it contains zero in beginning  of the number.
  4. 00531 is also not a Duck number because it contains leading zeros.

Algorithm to find Duck Number

  1. Take number variable (num) .
  2. check last digit of given number(num) is zero then return true .
  3. Remove the last digit from number(num).
  4. repeat step 2 & 3 until number(num) become zero.

Duck Number Program In Java

We will implement above algorithm to find duck number in java . 

package com.demo.program;
import java.util.Scanner;
   
public class DuckNumber {  
	
	
     //check given number is duck or not
   public static boolean isDuckNumber(int num) {  

	    
	   //iterate loop until num become zero
      while(num != 0) {  
  
         if(num%10 == 0)  
            return true;    //return true if the number is Duck  
  
         // divide the number by 10 to remove the last digit  
         num = num / 10;  
      }  
  
      return false; //return false if the number is not Duck  
   }  

    public static void main(String args[])   
    {     
        int num;  
          
        //create scanner object to get input from user  
        Scanner sc=new Scanner(System.in);  
          
        System.out.println("Enter number");  
          
        //take input from user   
        num = sc.nextInt();  

        if (isDuckNumber(num))   
            System.out.println("given number "+ num + " is a Duck number");   
        else  
            System.out.println("given number "+ num + " is not a Duck number");   
        
        //close scanner object 
        sc.close();
       
    }  
}

Output:

Enter number
2304
given number 2304 is a Duck number

Find all Duck Number in given Range

We will find duck number in java in given range . We take input of min range and max range from user and print all duck number in range .

package com.demo.program;

import java.util.Scanner;

public class DuckNumberInRange {
  
	  
	     //check given number is duck or not
	   public static boolean isDuckNumber(int num) {  
	  
		   //iterate loop until num become zero
	      while(num != 0) {  
	  
	         if(num%10 == 0)  
	            return true;    //return true if the number is Duck  
	  
	         // divide the number by 10 to remove the last digit  
	         num = num / 10;  
	      }  
	  
	      return false; //return false if the number is not Duck  
	   }  

	    public static void main(String args[])   
	    {     
	        int minRange , maxRange;  
	          
	        //create scanner object to get input from user  
	        Scanner sc=new Scanner(System.in);  
	          
	        System.out.println("Enter Min range");  
	          
	        //take user input   
	        minRange = sc.nextInt();  
	        
	        System.out.println("Enter Max range");  
	          
	        //take user input   
	        maxRange = sc.nextInt();  
	        
	        for (int i = minRange; i <= maxRange; i++) {
		        if (isDuckNumber(i))   
		            System.out.println( i + " is a duck number");   
				
			}
	          
	        //close scanner object 
	        sc.close();
	       
	    }  
	
}

Output:

Enter Min range
0
Enter Max range
50
10 is a duck number
20 is a duck number
30 is a duck number
40 is a duck number
50 is a duck number

We learned in this tutorial what is duck number in java , how to find duck number in java and print all duck number in range . You can see more java coding program for practice .

Leave a Reply

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

− 1 = 1