Computer Science, asked by Anonymous, 5 months ago

WAP in BlueJ to accept a number and check whether the number is duck number or not.​

Answers

Answered by anindyaadhikari13
2

Question:-

Write a program in BlueJ to accept a number and check whether it is a duck number or not.

A Duck number is a positive number which has zeroes present in it, note that a numbers with only leading 0s is not considered as Duck Number.

Program:-

import java.util.*;

class Duck

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter a positive number: ");

String s=sc.next();

boolean x=false;

for(int i=1;i<s.length();i++)

{

char ch=s.charAt(i);

if(ch=='0')

{

x=true;

break;

}

}

if(x)

System.out.println("Duck Number. ");

else

System.out.println("Not a Duck Number.");

}

}

Explanation:-

In this program, I have converted the number into string and then checked whether any one of the characters excluding the first index is 0 or not. If it's zero then it is a duck number else not.

Consider a number, say 12340

Number:- 1 2 3 4 0

Index:- 0 1 2 3 4

For each index starting from 1, I have checked if the number is zero or not.

Answered by JBJ919
0

Answer:

import java.util.Scanner;

public class duckNum

{

   public static void main ()

   {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter A number");

       int n = sc.nextInt();

       int d,n1=n,c=0;

       while (n>0)

       {

           d=n%10;

           if(d==0)

           {

               c++;

               break;

           }

           d=n/10;

           if(c>0)

           {

               System.out.println(n1+"It is a duck num");

           }

           else

           {

               System.out.println(n1+"it is not a duck num");

           }

       }

   }

}

Explanation:

Similar questions