KnowledgeBoat Logo

Java Number Programs (ICSE Classes 9 / 10)

A number is said to be Duck if the digit zero is (0) present in it. Write a program to accept a number and check whether the number is Duck or not. The program displays the message accordingly. (The number must not begin with zero)
Sample Input: 5063
Sample Output: It is a Duck number.
Sample Input: 7453
Sample Output: It is not a Duck number.

Java

Java Iterative Stmts

ICSE

10 Likes

Answer

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class KboatDuckNumber
{
    public void duckNumberCheck() throws IOException {
        
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);
        System.out.print("Enter number: ");
        boolean isDuck = false;
        boolean firstZero = false;
        int c = 0, d; 
        
        /*
         * 10 is the ASCII code of newline
         * We will read from inputstream
         * one character at a time till we
         * encounter a newline i.e. enter
         */
        while((d = in.read()) != 10) {
            
            char ch = (char)d;
            
            if (c == 0 && ch == '0'  ) 
                firstZero = true;
            
            if (!firstZero && ch == '0')
                isDuck = true;
                
            c++;
        }
        
        if (isDuck)
            System.out.println("Duck Number");
        else
            System.out.println("Not a Duck Number");
    }
}

Output

BlueJ output of A number is said to be Duck if the digit zero is (0) present in it. Write a program to accept a number and check whether the number is Duck or not. The program displays the message accordingly. (The number must not begin with zero) Sample Input: 5063 Sample Output: It is a Duck number. Sample Input: 7453 Sample Output: It is not a Duck number.

Answered By

2 Likes