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.

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.

Number

Contains Zero

First Digit Zero

Duck Number

52706

YES

NO

YES

0956

YES

YES

NO

7860

YES

NO

YES

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.

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

Take the input from the user. Initialize a variable to zero.

Take the length of the number and iterate through it.

Check if the number contains zero or not. It does increase the value of the variable.

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.

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

Take the input from the user and send it to the function to check the number.

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.

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

Take the input from the user.

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.

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

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: