Java program to check the given number is binary or not

Binary number system relied on base number 2 in the number system. In general, the base-2  system is a positional notation with a radix of 2. Consists of 0 and 1 both digit is referred to as a bit. Based on boolean output values here is a Java program for checking binary numbers.

BinaryorNot.java

public class BinaryorNot 
{
	public boolean isBinaryNumber(int binary)
	{
		boolean status = true;
		while(true) 
		{
			if(binary == 0) 
			{
				break;
			}
			else 
			{
				int tmp = binary%10;
				if(tmp > 1) 
				{
					status = false;
					break;
				}
				binary = binary/10;
			}
		}
		return status;
	}
	public static void main(String a[]) 
	{
		Base13 mbc = new Base13();
		System.out.println("Is 1000 binary? :"+mbc.isBinaryNumber(1000));
		System.out.println("Is 1030 binary? :"+mbc.isBinaryNumber(1030));
	}
}

Output:

Is 1000 binary? :true
Is 1030 binary? :false

Leave a Reply

Your email address will not be published. Required fields are marked *