###### Write a Program to check if given number is binary or not

This article will help you to understand “java program to check binary number”.

In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one).

The base-2 system is a positional notation with a radix of 2. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers and computer-based devices.

Each digit is referred to as a bit.

###### Java Program to check if given number is binary or not

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.codenuclear; public class CheckForBinary { public boolean isNumberBinary(int num) { boolean isBinary = true; while(true) { if(num == 0) { break; } else { int digit = num%10; if(digit > 1) { isBinary = false; break; } num = num/10; } } return isBinary; } public static void main(String ags[]) { CheckForBinary obj = new CheckForBinary(); System.out.println("Is number 1001100101 binary? : "+obj.isNumberBinary(1001100101)); System.out.println("Is number 001201010 binary? : "+obj.isNumberBinary(001201010)); } } |

Output

Some theory part of this article uses material from the Wikipedia article “Binary Number”, which is released under the CC BY-SA 3.0.