Showing posts with label check-duck-no. Show all posts
Showing posts with label check-duck-no. Show all posts

Wednesday, February 8, 2017

Write a Java Program to display all Duck Number between 1 to 500 .

Note: 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. */

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Duck2
{
public static void main(String...alt)throws Exception
{
for(int j=1;j<=500;j++)
{
int count=0;
String n=Integer.toString(j);
for(int i=0;i<n.length();i++)
if(n.charAt(i)=='0')
count++;
if(count>0)
System.out.println(j);
}
}
}

Write a Program in Java to input a number and check whether it is a Duck Number or not.

Note: 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. */

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Duck
{
public static void main(String...alt)throws Exception
{
int count=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
out.println("Enter a Number : ");
String n=br.readLine();
for(int i=0;i<n.length();i++)
if(n.charAt(i)=='0')
count++;
if(n.charAt(0)=='0' && count>0)
System.out.println(n+" is not duck no.");
else
System.out.println(n+" is duck no.");
}
}