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
- 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);
}
}
}
Output
The output of the above codes is shown below:
Enter a number
52790
Number is a duck number: 52790
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.
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: