हिंदी

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

प्रश्न

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
कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 1: Revision of Class 9 Syllabus - Exercises [पृष्ठ ४९]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 1 Revision of Class 9 Syllabus
Exercises | Q 8. | पृष्ठ ४९
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×