ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2024 ICSE Computer Science Specimen Paper



Share with a Friend

Solved 2024 ICSE Computer Science Specimen Paper

Class 10 - ICSE Computer Science Solved Specimen Papers

Duck Number Program - ICSE 2024 Computer Science Specimen Paper

Question 7

Define a class to accept a 3-digit number and check whether it is a duck number or not.

Note: A number is a duck number if it has zero in it.

 

Example 1:


INPUT: 2083
OUTPUT: Invalid

Example 2:


INPUT: 103
OUTPUT: Duck number

import java.util.Scanner; class Duck{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the number: "); int n = Integer.parseInt(in.nextLine()); if(n < 100 || n > 999){ System.out.println("Invalid"); return; } boolean isDuck = false; if(n == 0) isDuck = true; while(n != 0){ int digit = n % 10; if(digit == 0){ isDuck = true; break; } n /= 10; } if(isDuck) System.out.println("Duck number"); else System.out.println("Not a duck number"); } }

Output

 
 OUTPUT 1: 
Enter the number: 2083
Invalid
 
 OUTPUT 2: 
Enter the number: 103
Duck number