In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one). The base-2 system is a positional notation with a radix of 2. Below we have write a java program for check given number binary or not.
package com.javaproficiency;
public class IsBinary {
public static void main(String[] args) {
IsBinary isBinary = new IsBinary();
System.out.println(" num 101000 is binary = "+ isBinary.isBinary(101000) );
System.out.println(" num 101300 is binary = "+ isBinary.isBinary(101300) );
System.out.println(" num 101900 is binary = "+ isBinary.isBinary(101900) );
System.out.println(" num 10001 is binary = "+ isBinary.isBinary(10001) );
}
public boolean isBinary( int num ){
boolean flag = true;
while (num > 0) {
int rem = num %10;
if (rem > 1) {
flag = false;
break;
}
num = num/10;
}
return flag;
}
}
package com.javaproficiency;
public class IsBinary {
public static void main(String[] args) {
IsBinary isBinary = new IsBinary();
System.out.println(" num 101000 is binary = "+ isBinary.isBinary(101000) );
System.out.println(" num 101300 is binary = "+ isBinary.isBinary(101300) );
System.out.println(" num 101900 is binary = "+ isBinary.isBinary(101900) );
System.out.println(" num 10001 is binary = "+ isBinary.isBinary(10001) );
}
public boolean isBinary( int num ){
boolean flag = true;
while (num > 0) {
int rem = num %10;
if (rem > 1) {
flag = false;
break;
}
num = num/10;
}
return flag;
}
}
Output:
num 101000 is binary = true
num 101300 is binary = false
num 101900 is binary = false
num 10001 is binary = true
No comments:
Post a Comment