Given a number **N**, the task is to check first whether the given number is binary or not and its value should be greater than 1. print **true** if **N** is the binary representation else print **false**.

**Examples:**

Input:N = 1010

Output:true

Explanation:

Given number is greater than 1 and none of its digits is greater than 1. Thus, it is a binary number greater than 1.

Examples:N = 1234

Output:false

Explanation:

Given number is greater than 1 but some of its digits { 2, 3, 4} are greater than 1. Thus, it is not a binary number.

**Iterative Approach:**

- Check if the number is less than or equal to 1. If it is then, print false.
- Else if number is greater than 1 then, check if every digits of the number is
**1 or 0**. - If any digits of the number is greater than
**1**then print**false**, else print**true**.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` `class` `GFG { ` ` ` ` ` `// Function to check if number ` ` ` `// is binary or not ` ` ` `public` `static` `boolean` `isBinaryNumber(` `int` `num) ` ` ` `{ ` ` ` ` ` `// Return false if a number ` ` ` `// is either 0 or 1 or a ` ` ` `// negative number ` ` ` `if` `(num == ` `0` `|| num == ` `1` ` ` `|| num < ` `0` `) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Get the rightmost digit of ` ` ` `// the number with the help ` ` ` `// of remainder '%' operator ` ` ` `// by dividing it with 10 ` ` ` `while` `(num != ` `0` `) { ` ` ` ` ` `// If the digit is greater ` ` ` `// than 1 return false ` ` ` `if` `(num % ` `10` `> ` `1` `) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `num = num / ` `10` `; ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = ` `1010` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.println(isBinaryNumber(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

true

**Time Complexity:** *O(K)*, K is the number of digits in N

**Auxiliary Space:** *O(1)*

**Regular Expression Approach:**

- Convert the number into string.
- Create a Regular Expression as mentioned below:

regex = “[01][01]+”;

where:

**[01][01]**matches one of the following { “00”, “01”, “10”, “11” }.**+**matches one or more occurance of previous regular expression

- Match the given number with the regular expression. If it matches return true, else return false.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` `import` `java.util.regex.*; ` `class` `GFG { ` ` ` ` ` `// Function to check number is ` ` ` `// binary or not ` ` ` `public` `static` `boolean` `isBinaryNumber(` `int` `num) ` ` ` `{ ` ` ` ` ` `// Regex to check a number ` ` ` `// is binary or not ` ` ` `String regex = ` `"[01][01]+"` `; ` ` ` ` ` `// Match the given number with ` ` ` `// the regular expression ` ` ` `return` `Integer ` ` ` `.toString(num) ` ` ` `.matches(regex); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// Given Number ` ` ` `int` `N = ` `1010` `; ` ` ` `System.out.println(isBinaryNumber(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

true

**Time Complexity:** *O(K)*, K is the number of digits in N

**Auxiliary Space:** *O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check whether the binary equivalent of a number ends with "001" or not
- Check if binary representation of a number is palindrome
- Check if a given number is a Perfect square using Binary Search
- Check if binary representation of a given number and its complement are anagram
- Check whether the binary equivalent of a number ends with given string or not
- Java Program for How to check if a given number is Fibonacci number?
- Java Program to Count number of binary strings without consecutive 1's
- Java Program to check Armstrong Number
- Java program to check if a number is prime or not
- Java Program to Check if all digits of a number divide it
- Check if binary representations of 0 to N are present as substrings in given binary string
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Check if the given Binary Expressions are valid
- Minimum number of distinct powers of 2 required to express a given binary number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Number of ways to split a binary number such that every part is divisible by 2
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if B can be formed by permuting the binary digits of A
- Check if all elements of binary array can be made 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.