Table of contents
1.
Introduction 
2.
What is Duck Number in Java?
2.1.
52607
2.2.
0310
2.3.
6270
3.
Methods to Check Duck Numbers in Java 
3.1.
Method 1: Using For Loop
3.1.1.
Explanation
3.2.
Method 2. Using While Loop 
3.2.1.
Explanation
3.3.
Method 3. Using Functions
3.3.1.
Output
3.3.2.
Explanation
4.
Approach for Saying Number is Duck or not in Java? 
5.
Frequently Asked Questions
5.1.
What is a duck number in Java?
5.2.
What is a special number in Java?
5.3.
What is Krishnamurthy's number in Java?
6.
Conclusion
Last Updated: Nov 28, 2024
Medium

Duck Number in Java

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

A Duck number is a positive integer that contains at least one zero, except for those with starting zeros. For examples include 6302,6540, 7670768, and 10209. Numbers like 045 or 0010 are not considered Duck Numbers.

Duck Number in Java

In this blog, we will learn about the duck number in Java. We will understand this through the example, and we will also illustrate this program through code. 

What is Duck Number in Java?

Duck numbers are special numbers that do not have their number beginning with 0 digits but do have zero digits in their number. 

The duck numbers can have zero in the middle, in the end, but not at the beginning of the number. 

Number Contains ZeroFirst Digit Zero Duck Number
52706YESNOYES
0956YESYESNO
7860YESNOYES

To check whether the number is a duck number, we should analyze it and see that it must contain a zero digit.

To better understand Duck Numbers in Java, let's go through the examples below:

52607

  • The number we have is: 52607
     
  • The number has zero: Yes
     
  • The number begins with zero: No
     
  • The number is Duck Number: Yes 
     

It fulfills all the criteria to call a number a duck number.

0310

  • The number we have is: 0310
     
  • A number has zero: Yes
     
  • The number begins with zero: Yes 
     
  • The number is Duck Number: No 
     

It does not fulfill all the criteria to call a number a duck number.

6270

  • The number we have is: 6270
     
  • A number has zero: Yes
     
  • The number begins with zero: No
     
  • The number is Duck Number: Yes
     

It fulfills all the criteria to call a number a duck number.

Methods to Check Duck Numbers in Java 

In this, we have implemented the algorithm to check the number.  

Method 1: Using For Loop

Below is the implementation of the program to check the number. The code below is to check the duck number in java using for loop. 

import java.util.*;
public class Main
{
    public static boolean checkTheNumber(String number){
        int stringLength = number.length();
        int zero=0;
        char character;
        for(int i= 0; i < stringLength; i++){
            character = number.charAt(i);
            if(character == '0'){
                zero++;
            }
        }
        char firstDigit = number.charAt(0);
        if(firstDigit!= '0' && zero>0){
            return true;
        }
        else{
            return false;
        }
    }
    
    
    public static void main(String[] args) {
        
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter a number");
        String number= sc.nextLine();
        boolean result= checkTheNumber(number);
        if(result== true){
             System.out.println("Number is a duck number: "+ number);
        }
        else{
             System.out.println("Number is not a duck number: "+ number);


        }
    }
}


Explanation

  1. Take the input from the user. Initialize a variable to zero.
  2. Take the length of the number and iterate through it. 
  3. Check if the number contains zero or not. It does increase the value of the variable. 
  4. State the condition to check the first digit of the number. If the condition is true and the variable's value is greater than zero, it is a duck number.
  5. Print the number as a duck number.

Method 2. Using While Loop 

Below is the implementation of the program to check the number. The code below is to check the duck number in java using a while loop. 

import java.util.*;
public class Main
{
    public static boolean checkTheNumber(int inputNumber){
        while(inputNumber!=0) {
            if((inputNumber% 10) == 0){
                return true;
            }
            inputNumber = inputNumber/10;
                    }
                    return false;
    }
public static void main(String[] args) {
        
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter a number");
         int inputNumber= sc.nextInt();
        boolean result= checkTheNumber(inputNumber);
        if(result== true){
            System.out.println("Number is a duck number: "+ inputNumber);
        }
        else{
     System.out.println("Number is not a duck number: "+ inputNumber);


        }
    }
}


Explanation

  1. Take the input from the user and send it to the function to check the number. 
  2. In the function, divide the number by ten till the number is not zero. If the number contains zero, return true; if not, return false from the function to the main function. 
  3. If the return is true, print the number as the duck number; if the return is false, then print the number as not a duck number. 

Method 3. Using Functions

Below is the implementation of the program to check the number. The code below is to check the duck number in java using java functions

import java.util.*;
public class Main
{
public static void main(String[] args) {
        
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter a number");
        String number= sc.nextLine();
if(number.contains("0") && !number.startsWith("0")){
              System.out.println("Number is a duck number: "+ number);
         }
         else{
             System.out.println("Number is not a duck number: " + number);
         }
    }
}

Output

The output of the above codes is shown below:

Enter a number 
52790
Number is a duck number: 52790

Explanation

  1. Take the input from the user. 
  2. Next, we have defined the if statement, which has predefined java functions. The functions are contains and startsWith. When both the conditions are true, if the statement turns out true, print the number as a duck number.
  3. If the condition is false, the else statement runs. In the else statement, we have printed that the number is not a duck number.

Approach for Saying Number is Duck or not in Java? 

  • To check if a number is a duck number or not, we need to find the digits of the number.  
  • We can also use functions present in java to check further whether a number is a duck number.  
  • Duck numbers are non-zero positive numbers that have zero in them. 
  • It must not have zero in the beginning. 
  • Zero digits can be present in the middle, between, or at the end of the number.  

There are various ways to determine whether a number is a duck number. 

We have discussed below some of the algorithms you can use to find whether a number is a duck number. 

Check out this article - Upcasting and Downcasting in Java

Frequently Asked Questions

What is a duck number in Java?

A Duck Number is a number that doesn't start with a zero but has at least one digit as zero. For example, 102 is a Duck Number, whereas 0123 is not.

What is a special number in Java?

In Java, a Special Number is a positive integer whose sum of factorial digits equals the original number. Let's take an example, 145 is a Special Number because 1! + 4! + 5! = 145. This can be checked using basic arithmetic operations and loops in Java.

What is Krishnamurthy's number in Java?

A Krishnamurthy number is a number in Java whose sum of the factorials of its digits is equal to the number itself. Let's take an example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 145.

Conclusion

In this article, we have discussed the duck number in Java. We have seen how the position of zero matters in a number. We have looked at different ways of solving this particular problem. 

To learn more about Java, refer to the blogs mentioned below:

Live masterclass