Home >Java >javaTutorial >Duck Number in Java

Duck Number in Java

王林
王林Original
2024-08-30 15:15:22506browse

The following article provides an outline for Duck Number in Java. Duck Number is a number where zeros are present, but the zeros must not be present at the beginning of the number. A duck number is a positive non-zero number which has zero present in it. The zero is present at any position of that number except at the starting of the number. For example, 5103, 70139, 201407 are all duck numbers, whereas 0978,0355,08125 are not duck numbers.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The syntax of the duck number is as follows; there are several ways to check the number whether is a duck number. Let’s see one of the formats below,

checkZero =n.charAt(i); //to check whether 0 present
char f=n.charAt(0); //takes first digit of the number
if(checkZero =='0'&& f!='0')
"Duck Number".
else
"Not Duck Number"

How does Duck number work in Java?

A duck number is a positive non-zero number which has zero present in it. The zero is present at any position of that number except at the starting of the number. Let’s see the examples of duck numbers,

For example:

4560 is a duck number because it has the zero, which presents at the end of the number, but the zero is not present at starting of that number

09889 is not a duck number because it contains zero at starting of the number.

Duck Number – Algorithm

  • Firstly to calculate the length of a given number, i.e., inputted number.
  • To initialize checkZero variable value to 0, it represents the number of zero digits in the inputted number. To use for loop to check until condition satisfy.
  • To check one digit at a time of inputted number to check whether the digit equals to ‘0’. Increase checkZero to 1 if the digit equals to ‘0’.
  • To store the value of the first digit of an inputted number, which beginsZero
  • To check whether the variable beginsZero is not equaled to ‘0’ and the checkZero is greater than 0.
  • Display Duck Number if both conditions are true, otherwise display Not Duck Number.

Sample code

int length_=inputValue.length();
int checkZero=0;
char ch;
for(int i=1;i<1;i++)
{
checkZero =n.charAt(i); //to check whether 0 present
if(ch=='0')
checkZero++;
}
char f=n.charAt(0); //takes first digit of the number
if(checkZero>0 && f!='0')
system.out.println( "Duck Number");
else
system.out.println("Not Duck Number");

The above code follows the above algorithm to check or find the number is Duck Number, the main thing to find out the number is based on the ‘0’ appearance. If the number contains zero in it except in the beginning position, it is called a Duck Number. On the other hand, if the number contains zeros at the beginning position or without zero appearance, then it is Not a Duck Number.

Examples of Duck Number in Java

Duck number is a number where zeros are present in it, but the zeros must not be present at the beginning of the number. A duck number is a positive non-zero number which has zero present in it. Let’s see the example programmatically,

Example #1

Code:

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("Check Whether Entered Number is Duck Nnumber");
Scanner scan = new Scanner(System.in);
System.out.print("Enter Number : ");
String nstr = scan.nextLine();
int len = nstr.length();
int ctr = 0;
char checkZero;
for(int i=1;i<len;i++)
{
checkZero = nstr.charAt(i);
if(checkZero=='0')
ctr++;
}
char firstNumber = nstr.charAt(0);
if(ctr>0 && firstNumber!='0')
System.out.println("Duck number");
else
System.out.println("Not Duck Number");
}
}

In this above program, first, it gets the number from the user, then it starts checks the length of that number and based on the length it checks whether zero appearance checkZero = nstr.charAt(i) by using for a loop until condition satisfies. Then it checks whether zero appears in the first position firstNumber!=’0′ if both the condition satisfies it displays as Duck Number; otherwise, it’s Not Duck Number.

Output:

Duck Number in Java

Example #2

Code:

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
static boolean To_Check_DNumber(String num)
{
// to check the '0' appearance
int i = 0,
numValue = num.length();
while (i < numValue && num.charAt(i) == '0')
i++;
// to check the remaining digits
while (i < numValue)
{
if (num.charAt(i) == '0') // return true, if there is no '0' present at beginning place
return true;
i++;
}
return false;
} // main method
public static void main(String args[]) throws IOException
{
String inputValue = "70885";
if (To_Check_DNumber(inputValue)) // if it returns true
System.out.println("Duck Number");
else
System.out.println("Not Duck Number");
}
}

In the above program, it simply checks the number by using the Boolean Function; in that function itself, we check whether the passing number is ‘duck value’ in this program itself, we initialized the input_value that number is passed to the bool function based on the condition it returns either true or false. In the main function (), we called only the Boolean function To_Check_DNumber(inputValue); it displays “Duck Number” it condition is true.

Output:

Duck Number in Java

Example #3

Code:

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int rangeValue; // we can input the range value till that it checks whether the Duck_Numbers present
Scanner scan=new Scanner(System.in);
System.out.println("Enter Range_value: "); //here we need to enter the range value
rangeValue = scan.nextInt(); //to store entered value into variable
for(int i = 1; i <= rangeValue; i++)
if(check_Number(i))
{
System.out.println(i + " Duck number");
}
} // to create function call - checkNumber() which returns true if is Duck_Number
public static boolean check_Number(int getNumber)
{
while(getNumber != 0) // repeat loop until it satisfies condition
{
if(getNumber%10 == 0) // to check whether the number contains zero
return true; // if the number contains 'zero' in it then it returns true - and the Number is Duck
getNumber = getNumber / 10;
}
return false; //return false if the particular number is Not Duck Number
}
}

In this program, we can input the range value till that it checks whether the Duck_Numbers are present.

Output:

Duck Number in Java

Conclusion

In this article, we came to know how to find the Duck Numbers in Java, which is based on the availability of the zero. So we hope this article would help out to check whether the number is Duck Numbers.

The above is the detailed content of Duck Number in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn