Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: To Check Whether the Number is a Duck Number


29. Create a program in Java to find out if a number entered by the user is a Duck Number.

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, 6710, 8066, 5660303 are all duck numbers whereas 05257, 080009 are not.

🦆 What is a Duck Number?

A Duck Number:

  • Contains at least one zero (0)
  • Does NOT start with zero

Examples

Duck Numbers: 6710, 8066, 5660303
✘ Not Duck Numbers: 05257, 080009

 

import java.util.Scanner;

 

public class DuckNumber {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        String num;

        boolean hasZero = false;

 

        System.out.print("Enter a number: ");

        num = sc.nextLine();

 

        // Check if the number starts with 0

        if (num.charAt(0) == '0') {

            System.out.println(num + " is NOT a Duck Number");

            return;

        }

 

        // Check for presence of zero in the number

        for (int i = 0; i < num.length(); i++) {

            if (num.charAt(i) == '0') {

                hasZero = true;

                break;

            }

        }

 

        if (hasZero)

            System.out.println(num + " is a Duck Number");

        else

            System.out.println(num + " is NOT a Duck Number");

    }

}

Output

Sample Run 1 
Enter a number: 8066
8066 is a Duck Number

Sample Run 2 
Enter a number: 05257
05257 is NOT a Duck Number

Sample Run 3 
Enter a number: 12345
12345 is NOT a Duck Number

Explanation

  • Input is taken as a String to easily check digits.
  • If the first character is ‘0’, the number is immediately rejected.
  • The program scans the entire number to check for at least one zero.
  • If found → Duck Number.