Last Updated: Mar 27, 2024
Difficulty: Medium

Duck Number in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Want to learn more new coding problems in Java? If yes, then you're at the right place. Duck number in Java is a famous coding interview question for freshers.

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?

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

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.

Must Read Type Conversion in Java

Program 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);
}
}
}``````

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.

Output

The output of the above codes is shown below:

``````Enter a number
52790
Number is a duck number: 52790``````

Try it by yourself on java online compiler.

Must Read What are Loops in Java.

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.

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.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our coursesrefer to the mock test and problems look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Topics covered
1.
Introduction
2.
What is Duck Number?
3.
What is Duck Number in Java?
3.1.
52607
3.2.
0310
3.3.
6270
4.
Program in Java
4.1.
Method 1: Using For Loop
4.1.1.
Explanation
4.2.
Method 2. Using While Loop
4.2.1.
Explanation
4.3.
Method 3. Using Functions
4.3.1.
Explanation
4.3.2.
Output
5.
Approach for Saying Number is Duck or not in Java?
6.