Showing posts with label Duck Number. Show all posts
Showing posts with label Duck Number. Show all posts

Tuesday, 30 July 2024

Duck Number - Java

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, 1260, 70910 are all duck numbers whereas 03210, 025980 are not.

Let's see the Java program to determine if a number is DUCK Number or not.

import java.util.Scanner;

class HelloWorld 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        String n;
        char ctr = 0;
        char chr = ' ';
        System.out.println("Enter a number");
        n = sc.nextLine();
        int length = n.length();
        for(int i = 1;i<length;i++)
        {
            chr = n.charAt(i);
            if(chr == '0')
            {
                ctr++;
            }
        }
        char firstindex = n.charAt(0);
        if(ctr > 0 && firstindex!='0')
        {
            System.out.println("Duck number");
        }
        else
        {
            System.out.println("Not a Duck number");
        }
    }
}