English

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:

Advertisements
Advertisements

Question

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
Code Writing
Advertisements

Solution

import java.util.Scanner;
class DuckNumber
{
    public static void main(String args[])
    {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter a 3-digit number");
       int n=sc.nextInt();
       if(n>=100 && n<=999)//checking for 3-digit number
       {
         int m=n;//temporary storage for the loop
         int p=1;
         while(m>0)
       {
            int r=m%10;//extracting each digit from the number
            p*=r; //multiplying the digits
            m=m/10;//storing the remaining number
       }
       if(p==0)
            System.out.println("Duck number");
       else
            System.out.println("Not a duck number");
       }
       else
            System.out.println("Invalid number");
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 49]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 8. | Page 49
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×