WAP to take a number as input check it is 4- digit duck number or not.
Eg:A Duck number is a positive number which has zeroes present in it, For example 3210, 8050896, 70709 ( JAVA PROGRAMMING )
PLS HELP!
Answers
import have.util.*;
public class and
{
public static void main()
{
Scanner class= new Scanner ( System.in);
int n, z , i;
System.out.println( " enter the number");
n= in.nextInt();
if (z= n%10==0)
System.out.println( " duck no.");
else
System.out.println( " not a duck no.");
}
}
Required Answer:-
Question:
- Write a program in Java to take a number as input and check if it is a duck number.
Solution:
Here is the code.
import java.util.*;
public class DuckNumber {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
boolean isDuck=false;
while(n!=0)
{
int d=n%10;
if(d==0)
{
isDuck=true;
}
n/=10;
}
if(isDuck)
System.out.println("Number is a duck number.");
else
System.out.println("Number is not a duck number.");
sc.close();
}
}
Explanation:
- A number is said to be a duck number if zero is present in it but not in the leftmost position. In this program, I have taken a number as input and then check if zero is present in it. How to check?? Using modulus operator, get the last digit of the number. If the number is zero, then it is a duck number. Now, divide the number by 10 to remove the last digit. Check again if zero is present. In this way, the program works.
- We assume that the number is not a duck number. If zero is found, it is a duck number.
Output is attached.