Duck number is a number that should contains zero (s) but at the beginning there should not be any zero 1230, 10208 are examples of Duck Number but 0987 is not. This is a BlueJ Program to check whether a number is a Duck Number or not.
import java.util.*;
public class Duck
{
Scanner sc=new Scanner(System.in);
int c=0,len;
String str1;
public void takeNumber()
{
System.out.print("\nEnter the number:");
str1=sc.nextLine();
len=str1.length()-1;
for(int i=1;i<=len;i++)
{
if(str1.charAt(i)=='0')
c++;
}
if(str1.charAt(0)!='0' && c >0)
System.out.print("\nEntered number is a duck number.");
else
System.out.print("\nEntered number is not a duck number.");
}
public static void main(String args[])
{
Duck obj=new Duck();
obj.takeNumber();
}
}
Related Post: BlueJ Programs on Number
No comments:
Post a Comment