Check if the given decimal number has 0 and 1 digits only - GeeksforGeeks

26 May, 2021
Given an integer n, the task is to check whether n is in binary or not. Print true if n is the binary representation else print false.
Examples: 
Input: n = 1000111 Output: true
Input: n = 123 Output: false  
Method #1: Using Set First add all the digits of n into a set after that remove 0 and 1 from the set, if the size of the set becomes 0 then the number is in binary format.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to check whether the given number// is in binary format#include<bits/stdc++.h>using namespace std;    // Function that returns true if given number    // is in binary format  i.e. number contains    // only 0's and/or 1's    bool isBinary(int number)    {        set<int> set;          // Put all the digits of the number in the set        while (number > 0) {            int digit = number % 10;            set.insert(digit);            number /= 10;        }          // Since a HashSet does not allow duplicates so only        // a single copy of '0' and '1' will be stored        set.erase(0);        set.erase(1);          // If the original number only contained 0's and 1's        // then size of the set must be 0        if (set.size() == 0) {            return true;        }        return false;    }      // Driver code    int main()    {        int n = 1000111;        if(isBinary(n)==1)            cout<<"true"<<endl;        else            cout<<"No"<<endl;    }//contributed by Arnab Kundu
// Java program to check whether the given number// is in binary formatimport java.util.HashSet;import java.util.Set; class GFG {     // Function that returns true if given number    // is in binary format  i.e. number contains    // only 0's and/or 1's    static boolean isBinary(int number)    {        Set<Integer> set = new HashSet<>();         // Put all the digits of the number in the set        while (number > 0) {            int digit = number % 10;            set.add(digit);            number /= 10;        }         // Since a HashSet does not allow duplicates so only        // a single copy of '0' and '1' will be stored        set.remove(0);        set.remove(1);         // If the original number only contained 0's and 1's        // then size of the set must be 0        if (set.size() == 0) {            return true;        }        return false;    }     // Driver code    public static void main(String a[])    {        int n = 1000111;        System.out.println(isBinary(n));    }}
# Python 3 program to check whether# the given number is in binary format # Function that returns true if given# number is in binary format i.e. number# contains only 0's and/or 1'sdef isBinary(number):    set1 = set()     # Put all the digits of the    # number in the set    while(number > 0):        digit = number % 10        set1.add(digit)        number = int(number / 10)     # Since a HashSet does not allow    # duplicates so only a single copy    # of '0' and '1' will be stored    set1.discard(0)    set1.discard(1)         # If the original number only    # contained 0's and 1's then    # size of the set must be 0    if (len(set1) == 0):        return True     return False     # Driver codeif __name__ == '__main__':    n = 1000111    if(isBinary(n) == 1):        print("true")    else:        print("No") # This code is contributed by# Surendra_Gangwar
     // C# program to check whether the given number// is in binary formatusing System;using System.Collections.Generic;public class GFG {      // Function that returns true if given number    // is in binary format  i.e. number contains    // only 0's and/or 1's    static bool isBinary(int number)    {        HashSet<int> set = new HashSet<int>();          // Put all the digits of the number in the set        while (number > 0) {            int digit = number % 10;            set.Add(digit);            number /= 10;        }          // Since a HashSet does not allow duplicates so only        // a single copy of '0' and '1' will be stored        set.Remove(0);        set.Remove(1);          // If the original number only contained 0's and 1's        // then size of the set must be 0        if (set.Count == 0) {            return true;        }        return false;    }      // Driver code    public static void Main()    {        int n = 1000111;        Console.WriteLine(isBinary(n));    }}//This code is contributed by Rajput-Ji
<script>// Javascript program to check whether the given number// is in binary format         // Function that returns true if given number    // is in binary format  i.e. number contains    // only 0's and/or 1's    function isBinary(number)    {        let set = new Set();           // Put all the digits of the number in the set        while (number > 0) {            let digit = number % 10;            set.add(digit);            number = Math.floor(number/10);        }           // Since a HashSet does not allow duplicates so only        // a single copy of '0' and '1' will be stored        set.delete(0);        set.delete(1);           // If the original number only contained 0's and 1's        // then size of the set must be 0        if (set.size == 0) {            return true;        }        return false;    }         // Driver code    let n = 1000111;    document.write(isBinary(n));          // This code is contributed by rag2127</script>
true
 
Method #2: Native Way 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check whether the// given number is in binary format#include<bits/stdc++.h>using namespace std; // Function that returns true if// given number is in binary format// i.e. number contains only 0's and/or 1'sint isBinary(int number){    while (number > 0)    {        int digit = number % 10;         // If digit is other than 0 and 1        if (digit > 1)            return false;        number /= 10;    }    return true;} // Driver codeint main(){  int n = 1000111;  if (isBinary(n) == 1)    cout << "true";  else    cout << "false"; // This code is contributed// by Shivi_Aggarwal}
// Java program to check whether the// given number is in binary format class GFG {     // Function that returns true if    // given number is in binary format    // i.e. number contains only 0's and/or 1's    static boolean isBinary(int number)    {        while (number > 0) {            int digit = number % 10;             // If digit is other than 0 and 1            if (digit > 1)                return false;            number /= 10;        }        return true;    }     // Driver code    public static void main(String a[])    {        int n = 1000111;        System.out.println(isBinary(n));    }}
# Python3 program to check whether the# given number is in binary format # Function that returns true if# given number is in binary format# i.e. number contains only 0's and/or 1'sdef isBinary(number):     while (number > 0):        digit = number % 10         # If digit is other than 0 and 1        if (digit > 1):            return False        number //= 10         return True # Driver codeif __name__ == "__main__":     n = 1000111    if (isBinary(n) == 1):        print ("true")    else:        print ("false") # This code is contributed by ita_c
// C# program to check whether the// given number is in binary format using System; class GFG {     // Function that returns true if    // given number is in binary format    // i.e. number contains only 0's and/or 1's    static bool isBinary(int number)    {        while (number > 0) {            int digit = number % 10;             // If digit is other than 0 and 1            if (digit > 1)                return false;            number /= 10;        }        return true;    }     // Driver code    static void Main()    {        int n = 1000111;        Console.WriteLine(isBinary(n));    }    // This code is contributed by Ryuga}
<?php// PHP program to check whether the// given number is in binary format // Function that returns true if// given number is in binary format// i.e. number contains only 0's and/or 1'sfunction isBinary($number){    while ($number > 0)    {        $digit = $number % 10;         // If digit is other than 0 and 1        if ($digit > 1)            return false;        $number /= 10;    }    return true;} // Driver code$n = 1000111;if (isBinary($n) == 1)    echo "true";else    echo "false"; // This code is contributed// by Mukul Singh
<script> // Javascript program to check whether the// given number is in binary format // Function that returns true if// given number is in binary format// i.e. number contains only 0's and/or 1'sfunction isBinary(number){    while (number > 0)    {        let digit = number % 10;         // If digit is other than 0 and 1        if (digit > 1)            return false;                     number = Math.floor(number / 10);    }    return true;} // Driver codelet n = 1000111; document.write(isBinary(n));     // This code is contributed by avanitrachhadiya2155 </script>
true
 
ankthon
Code_Mech
ukasp
Shivi_Aggarwal
andrew1234
SURENDRA_GANGWAR
Rajput-Ji
rag2127
avanitrachhadiya2155
number-digits
Technical Scripter 2018
Java Programs
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
Java Program to Read a File to String
How to Replace a Element in Java ArrayList?
Java Program to Find Sum of Array Elements
How to Write Data into Excel Sheet using Java?
Removing last element from ArrayList in Java
Tic-Tac-Toe Game in Java
Java Program to Write an Array of Strings to the Output Console