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?
