File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class ValidateBinary {
4
+
5
+ static String output = "" ;
6
+
7
+ public static void main (String [] args ) {
8
+
9
+ int num , binaryInput ;
10
+
11
+ System .out .println ("Program to check if the entered number is Binary Code or not.\n ---" );
12
+
13
+ Scanner sm = new Scanner (System .in );
14
+
15
+ System .out .print ("Enter the number: " );
16
+
17
+ binaryInput = sm .nextInt ();
18
+
19
+ //checkBinary(binaryInput);
20
+
21
+ if (checkBinary (binaryInput )) {
22
+
23
+ }
24
+ else {
25
+ System .out .println ("The number you entered is Binary Code." );
26
+ }
27
+ end ();
28
+ }
29
+
30
+ static boolean checkBinary (int n ) {
31
+ int x ;
32
+ boolean flag = false ;
33
+ while (n >0 ) {
34
+ x =n %10 ;
35
+
36
+ if (flag ==true ) {
37
+ System .out .println ("Entered String is not a binary number." );
38
+ break ;
39
+ }
40
+ else {
41
+ if (x ==1 || x ==0 ) {
42
+
43
+ }
44
+ else {
45
+ flag = true ;
46
+ }
47
+ }
48
+ n =n /10 ;
49
+ }
50
+
51
+ return flag ;
52
+
53
+ }
54
+
55
+ static void end () {
56
+ System .out .println ("---\n The Program has ended." );
57
+ System .exit (0 );
58
+ }
59
+
60
+
61
+ }
You can’t perform that action at this time.
0 commit comments