Algorithm for Check Number is Binary or Not:
step 1: Set r=0,c=0
step 2: Read num
step 3: Set b=num
step 4: Repeat through step-7 while num greater than 0
step 5: If (num mod 10)equals to 0 OR equals to 1 then c=c+1
step 6: r=r+ 1
step 7: num=num/ 1 0
step 8: If c equals to r then Print it is a binary number
Else print it is not a binary number
step 9:Exit
Here is the Java Example for Check Number is Binary or Not:
import java.util.Scanner;
public class CheckNumberisBinary
{
public static void main(String args[])
{
int r=0,c=0,num,b;
Scanner sl=new Scanner(System.in);
System.out.println("Enter a number");
num=sl.nextInt();
b= num;
while(num>0)
{
if((num %10==0)|| (num%10==1))
c++;
r++;
num=num/10;
}
if(c==r)
System.out.println(b+" is a Binary Number.");
else
System.out.println(b+" is not a Binary Number");
}
}