write a java program to check whether given number is binary or not

Solutions on MaxInterview for write a java program to check whether given number is binary or not by the best coders in the world

showing results for - "write a java program to check whether given number is binary or not"
Felipe
01 Mar 2020
1import java.util.Scanner;
2public class CheckNumberisBinary
3{
4            public static void main(String args[])
5        {
6                   int r=0,c=0,num,b;
7              Scanner sl=new Scanner(System.in);
8              System.out.println("Enter a number");
9              num=sl.nextInt();
10              b= num;
11              while(num>0)
12                   {
13                      if((num %10==0)|| (num%10==1))
14                         c++;
15                         r++;
16                         num=num/10;
17                   }
18                      if(c==r)
19               System.out.println(b+" is a Binary Number.");
20               else
21               System.out.println(b+" is not a Binary Number");
22           }
23}