In the previous article, we have discussed Java Program to Check Buzz Number
In this article we are going to understand what Duck number is and how we can check whether a number is Duck or not in Java with examples.
Java Program to Check Duck Number
Duck numbers are numbers which has zeroes in it however, numbers with leading zeroes are not duck numbers.
Example: 905 -> Duck Number 678 -> Not a Duck Number 009 -> Not Duck Number
In the above examples the number 905 and 009 has zeroes in it whereas 678 has no zeroes in it. Even though 009 has zeroes, they are leading zeroes so it is not a duck number either. Hence 905 is the only Duck number here.
Let’s see different ways to check Duck Number.
Approach:
- We ask the user to enter a number which we store in an integer variable
num
. - We then use a
while loop
to iterate through all the digits in the number and if a zero is encountered we set the flag to true and break from the loop. - If both flag is true, it is said to be a Duck number or else it is not.
Method-1:Java Program to Check Duck Number By Using Static Value
import java.util.*; public class DuckNumber{ public static void main(String args[]) { //a integer value declared int num = 9099; //Temp is used to preserve the original value int temp = num, remainder; //Flag is a variable to break the loop if 0 is encountered boolean flag = false; //While loop to iterate through each digit while (temp > 0) { remainder = temp % 10; if(remainder==0) { flag=true; } temp = temp / 10; } if(flag) { System.out.println(num+" is a Duck Number"); } else { System.out.println(num+" is not a Duck Number"); } } }
Output: 9099 is not a Duck Number
Method-2:Java Program to Check Duck Number By Using User Input Value
import java.util.Scanner; class DuckNumber{ public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Temp is used to preserve the original value int temp = num, remainder; //Flag is a variable to break the loop if 0 is encountered boolean flag = false; //While loop to iterate through each digit while (temp > 0) { remainder = temp % 10; if(remainder==0) { flag=true; } temp = temp / 10; } if(flag) { System.out.println(num+" is a Duck Number"); } else { System.out.println(num+" is not a Duck Number"); } } }
Output: Enter a number : 0079 79 is not a Duck Number
Method-2:Java Program to Check Duck Number By Using User Defined Method
import java.util.*; public class DuckNumber { public static void main(String args[]) { //a integer value declared int num = 9099; //calling the checkNumber() user defined method // to check whether the numbner is a duck number or not. checkNumber(num); } public static void checkNumber(int num) { //Temp is used to preserve the original value int temp = num, remainder; //Flag is a variable to break the loop if 0 is encountered boolean flag = false; //While loop to iterate through each digit while (temp > 0) { remainder = temp % 10; if(remainder==0) { flag=true; } temp = temp / 10; } if(flag) { System.out.println(num+" is a Duck Number"); } else { System.out.println(num+" is not a Duck Number"); } } }
Output: 9099 is a Duck Number
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: