-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuck_Number.java
More file actions
78 lines (62 loc) · 2.84 KB
/
Copy pathDuck_Number.java
File metadata and controls
78 lines (62 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Duck number is another special positive non-zero number that contains zero in
// it. The digit zero should not be presented at the starting of the number.
// Zero can be present at any of the positions except the beginning of the
// number.
// Let's understand some examples of Duck numbers.
// 3210 is a Duck number because it contains zero at the end of the number but
// not present at the beginning of it.
// 08237 is not a Duck number because it contains zero at the beginning of it.
// 7033 is a Duck number because it contains zero at the second position, not at
// the beginning.
// 030405 is not a Duck number because it also contains zero in starting of the
// number.
// 00153 is also not a Duck number because it contains leading zeros.
// These are the following steps that we use to check whether the given number
// is a Duck number or not.
// We first take a number.
// We then find the last digit of the number.
// If the last digit is zero, it is a Duck number.
// Otherwise, we remove that digit from the number.
// Perform steps 2, 3, and 4 until the number becomes zero.
// Let's implement the code to check a valid Duck number.
//import required classes and packages
import java.util.*;
import java.io.*;
import java.util.Scanner;
class Duck_Number {
// create checkNumber() method that returns true when it founds number Buzz
public static boolean checkNumber(int number) {
// use loop to repeat steps
while(number != 0) {
// check whether the last digit of the number is zero or not
if(number%10 == 0)
return true; //return true if the number is Duck
// divide the number by 10 to remove the last digit
number = number / 10;
}
return false; //return false if the number is not Duck
}
// main() method start
public static void main(String args[])
{
int n1, n2;
//create scanner class object to get input from user
Scanner sc=new Scanner(System.in);
//show custom message
System.out.println("Enter first number");
//store user entered value into variable n1
n1 = sc.nextInt();
//show custom message
System.out.println("Enter second number");
//store user entered value into variable n2
n2 = sc.nextInt();
if (checkNumber(n1))
System.out.println(n1 + " is a Duck number");
else
System.out.println(n1 + " is not a Duck number");
if (checkNumber(n2))
System.out.println(n2 + " is a Duck number");
else
System.out.println(n2 + " is not a Duck number");
}
}