- To check number is in binary or not
User Input: 11111
Output: Number is in binary form
import java.util.Scanner;
public class CheckBinary {
public static boolean binary(long num) {
while(num!=0) {
if(num%10==0 || num%10==1)
num/=10;
else return false;
}
return true;
}
public static void main(String []args) {
long num;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter your number : ");
num = userInput.nextLong();
if(binary(num))
System.out.println("Number is in binary form");
else
System.out.println("Number is not in binary form");
}
}
Enter Your number : 1101 Number is in binary formProgram
Binary Numbers : Numbers with only 0 & 1 combination
Examples like: 101, 111, 10, 11111
Main Logic :
public static int binary(long num) {
while(num!=0) {
if(num%10==0 || num%10==1)
num/=10;
else return false;
}
return true;
}
Divide the number by 10 while your number becomes 0[ZERO] &
In-between check, is there any remainder other than 0 & 1
Coming Soon !
QuickThe dreams I chased took me on a journey, a journey more rewarding than the goals