Java Program to check ‘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.
Ans:

import java.io.*;
class Duck_No
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter any number : “);
String n=br.readLine(); //inputting the number and storing it in a String
int l=n.length(); //finding the length (number of digit) of the number
int c=0; //variable for counting number of zero digits
char ch;
for(int i=1;i<l;i++) { ch=n.charAt(i); //extracting each digit and checking whether it is a ‘0’ or not if(ch==’0′) c++; } char f=n.charAt(0); //taking out the first digit of the inputted number if(c>0 && f!=’0′)
System.out.println(“It is a duck number”);
else
System.out.println(“It is 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

ICSE Geography Super Topographical Maps for Class 10 Board Exams - Digital Download
Bought by Jyotsna Singh from Bulandshahr
Total order for Rs. 199
about 11 hours ago
 
ICSE Geography Super Topographical Maps for Class 10 Board Exams - Digital Download
Bought by Dipendra narayan Choudhury from Kolkata
Total order for Rs. 199
about 12 hours ago
 
Exam18 ICSE Ultimate Guide to Much Ado About Nothing
ISC Class 12 Ultimate Guide to Much Ado About Nothing for March 2017 Board Exams - Digital Download
Bought by S J from Gurgaon
Total order for Rs. 149
about 14 hours ago
 
ICSE Geography Super Topographical Maps for Class 10 Board Exams - Printed Book + 2 Items
Bought by Nirmala Kumari from Ernakulam
Total order for Rs. 1,547
about 17 hours ago
 
ICSE English Language
ICSE English Language Last Step Practice for Class 10 Board Exams - Digital Download + 5 Items
Bought by Rashmi K from Malur
Total order for Rs. 1,394
about 24 hours ago
 

Want to prepare for Exams the easy way?

Tips and Tricks, Expert Teacher's Advice and Exclusive Books by Exam18.
Emails sent once every week. Only useful stuff and you will love it!