-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDuck Number
More file actions
53 lines (41 loc) · 1.56 KB
/
Duck Number
File metadata and controls
53 lines (41 loc) · 1.56 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
//import required classes and packages
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class DuckNumberExample {
// create checkNumber() method that returns true when it founds number Bu
//ghjhjkkhg
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;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
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");
}
}
//end of the code
//optimized code using bufferedReader