You are here:   HomeJava ProgrammingControl Structures in JavaEnter 10 Number and Print the Number of Positive, Negative and Zeros Number
by Dinesh Thakur Category: Control Structures

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");
           }
}

Check Number is Binary or Not





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article