Programming Examples
Java program for Duck number
Write a Program in Java to input a number and check whether it is a Duck Number or not.
A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not.
Solution
import java.util.Scanner;
public class DuckNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
String str = sc.nextLine();
int l = str.length();
int count = 0;
char chr;
for(int i=1;i<l;i++)
{
chr = str.charAt(i);
if(chr=='0')
count++;
}
char f = str.charAt(0);
if(count>0 && f!='0')
System.out.println("Duck number");
else
System.out.println("Not a duck number");
}
}
Output
Enter any number : 4013
It is a duck number
Enter any number : 0134
It is not a duck number
Enter any number : 67300987
It is a duck number