Program for Binary To Decimal Conversion

20 Jun, 2022
Given a binary number as input, we need to write a program to convert the given binary number into an equivalent decimal number.
Examples : 
Input : 111
Output : 7

Input : 1010
Output : 10

Input: 100001
Output: 33
The idea is to extract the digits of a given binary number starting from the rightmost digit and keep a variable dec_value. At the time of extracting digits from the binary number, multiply the digit with the proper base (Power of 2) and add it to the variable dec_value. In the end, the variable dec_value will store the required decimal number.For Example: If the binary number is 111. dec_value = 1*(2^2) + 1*(2^1) + 1*(2^0) = 7
The below diagram explains how to convert ( 1010 ) to equivalent decimal value:  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Below is the implementation of the above idea : 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to convert binary to decimal#include <iostream>using namespace std; // Function to convert binary to decimalint binaryToDecimal(int n){    int num = n;    int dec_value = 0;     // Initializing base value to 1, i.e 2^0    int base = 1;     int temp = num;    while (temp) {        int last_digit = temp % 10;        temp = temp / 10;         dec_value += last_digit * base;         base = base * 2;    }     return dec_value;} // Driver program to test above functionint main(){    int num = 10101001;     cout << binaryToDecimal(num) << endl;}
// Java program to convert// binary to decimal // Function to convert// binary to decimalclass GFG {    static int binaryToDecimal(int n)    {        int num = n;        int dec_value = 0;         // Initializing base        // value to 1, i.e 2^0        int base = 1;         int temp = num;        while (temp > 0) {            int last_digit = temp % 10;            temp = temp / 10;             dec_value += last_digit * base;             base = base * 2;        }         return dec_value;    }     // Driver Code    public static void main(String[] args)    {        int num = 10101001;        System.out.println(binaryToDecimal(num));    }} // This code is contributed by mits.
# Python3 program to convert# binary to decimal # Function to convert# binary to decimaldef binaryToDecimal(n):    num = n;    dec_value = 0;         # Initializing base    # value to 1, i.e 2 ^ 0    base = 1;         temp = num;    while(temp):        last_digit = temp % 10;        temp = int(temp / 10);                 dec_value += last_digit * base;        base = base * 2;    return dec_value; # Driver Codenum = 10101001;print(binaryToDecimal(num)); # This code is contributed by mits
// C# program to convert// binary to decimal // Function to convert// binary to decimalclass GFG {    public static int binaryToDecimal(int n)    {        int num = n;        int dec_value = 0;         // Initializing base1        // value to 1, i.e 2^0        int base1 = 1;         int temp = num;        while (temp > 0) {            int last_digit = temp % 10;            temp = temp / 10;             dec_value += last_digit * base1;             base1 = base1 * 2;        }         return dec_value;    }     // Driver Code    public static void Main()    {        int num = 10101001;         System.Console.Write(binaryToDecimal(num));    }} // This code is contributed by mits.
<?php// PHP program to convert// binary to decimal // Function to convert// binary to decimalfunction binaryToDecimal($n){    $num = $n;    $dec_value = 0;         // Initializing base value    // to 1, i.e 2^0    $base = 1;         $temp = $num;    while ($temp)    {        $last_digit = $temp % 10;        $temp = $temp / 10;                 $dec_value += $last_digit                        * $base;        $base = $base*2;    }    return $dec_value;}     // Driver Code    $num = 10101001;    echo binaryToDecimal($num), "\n"; // This code is contributed by ajit?>
<script> // JavaScript program to convert binary to decimal // Function to convert binary to decimalfunction binaryToDecimal(n){    let num = n;    let dec_value = 0;     // Initializing base value to 1, i.e 2^0    let base = 1;     let temp = num;    while (temp) {        let last_digit = temp % 10;        temp = Math.floor(temp / 10);         dec_value += last_digit * base;         base = base * 2;    }     return dec_value;} // Driver program to test above function    let num = 10101001;     document.write(binaryToDecimal(num) + "<br>");  // This code is contributed by Surbhi Tyagi </script>
Output: 
169
Time complexity : O( logn)
Auxiliary Space : O(1)
Note: The program works only with binary numbers in the range of integers. In case you want to work with long binary numbers like 20 bits or 30 bit, you can use a string variable to store the binary numbers.
Below is a similar program which uses string variable instead of integers to store binary value:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to convert binary to decimal// when input is represented as binary string.#include <iostream>#include <string>using namespace std; // Function to convert binary to decimalint binaryToDecimal(string n){    string num = n;    int dec_value = 0;     // Initializing base value to 1, i.e 2^0    int base = 1;     int len = num.length();    for (int i = len - 1; i >= 0; i--) {        if (num[i] == '1')            dec_value += base;        base = base * 2;    }     return dec_value;} // Driver program to test above functionint main(){    string num = "10101001";    cout << binaryToDecimal(num) << endl;}
// Java program to convert binary to// decimal when input is represented// as binary string.import java.io.*; class GFG {     // Function to convert binary to decimal    static int binaryToDecimal(String n)    {        String num = n;        int dec_value = 0;         // Initializing base value to 1,        // i.e 2^0        int base = 1;         int len = num.length();        for (int i = len - 1; i >= 0; i--) {            if (num.charAt(i) == '1')                dec_value += base;            base = base * 2;        }         return dec_value;    }     // Driver program to test above function    public static void main(String[] args)    {        String num = new String("10101001");        System.out.println(binaryToDecimal(num));    }} // This code is contribute by Prerna Saini
# Python3 program to convert binary# to decimal when input is# represented as binary string. # Function to convert# binary to decimaldef binaryToDecimal(n):    num = n;    dec_value = 0;         # Initializing base    # value to 1, i.e 2 ^ 0    base1 = 1;         len1 = len(num);    for i in range(len1 - 1, -1, -1):        if (num[i] == '1'):                dec_value += base1;        base1 = base1 * 2;         return dec_value; # Driver Codenum = "10101001";print(binaryToDecimal(num)); # This code is contributed by mits
// C# program to convert binary to// decimal when input is represented// as binary string.using System; class GFG {     // Function to convert binary to decimal    static int binaryToDecimal(String n)    {        String num = n;        int dec_value = 0;         // Initializing base value to 1,        // i.e 2^0        int base1 = 1;         int len = num.Length;        for (int i = len - 1; i >= 0; i--) {            if (num[i] == '1')                dec_value += base1;            base1 = base1 * 2;        }         return dec_value;    }     // Driver Code    public static void Main()    {        String num = "10101001";        Console.WriteLine(binaryToDecimal(num));    }} // This code is contribute by mits
<?php// PHP program to convert binary// to decimal when input is// represented as binary string. // Function to convert// binary to decimalfunction binaryToDecimal($n){    $num = $n;    $dec_value = 0;         // Initializing base    // value to 1, i.e 2^0    $base = 1;         $len = strlen($num);    for ($i = $len - 1; $i >= 0; $i--)    {        if ($num[$i] == '1')                $dec_value += $base;        $base = $base * 2;    }         return $dec_value;} // Driver Code$num = "10101001";echo binaryToDecimal($num), "\n"; // This code is contributed by aj_36?>
<script> // Javascript program to convert binary to decimal// when input is represented as binary string. // Function to convert binary to decimalfunction binaryToDecimal(n){    let num = n;    let dec_value = 0;     // Initializing base value to 1, i.e 2^0    let base = 1;     let len = num.length;    for (let i = len - 1; i >= 0; i--) {        if (num[i] == '1')            dec_value += base;        base = base * 2;    }     return dec_value;} // Driver program to test above function     let num = "10101001";    document.write(binaryToDecimal(num) + "<br>"); // This code is contributed by Mayank Tyagi </script>
Output : 
169
Time complexity : O(n) where n is the length of the string.
Auxiliary Space : O(1)
Using pre-defined function:  
C++
C
Java
Python3
C#
Javascript
#include <iostream>using namespace std; int main(){    char binaryNumber[] = "1001";         cout << stoi(binaryNumber, 0, 2);     return 0;}// This code is contributed by whysodarkbro
#include <stdio.h>#include <math.h>#include <stdlib.h> int main(){    char binaryNumber[] = "1001";    int bin, dec = 0;         bin = atoi(binaryNumber);     for(int i = 0; bin; i++, bin /= 10)        if (bin % 10)            dec += pow(2, i);     printf("%d", dec);     return 0;} // This code is contributed by whysodarkbro
public class GFG {    public static void main(String args[])    {        String binaryNumber = "1001";        System.out.println(Integer.parseInt(binaryNumber, 2));    }} // This code is contributed// By Yash Kumar Arora
n = input() # Convert n to base 2s = int(n, 2) print(s)
using System; class GFG {     public static void Main(){    int value = 1001;    Console.Write(Convert.ToInt32(value.ToString(), 2));}} // This code is contributed by SoumikMondal
<script>         var binaryNumber = "1001";document.write(parseInt(binaryNumber, 2)); // This code contributed by Princi Singh</script>
Output :  
9
Time complexity: O(n) where n is the length of the given string.
Auxiliary Space: O(1) This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Mithun Kumar
arora_yash
rgnithesh
SoumikMondal
whysodarkbro
surbhityagi15
pavanpal25878543
mayanktyagi1709
princi singh
devendrasalunke
Adobe
base-conversion
binary-representation
Computer Organization & Architecture
Digital Electronics & Logic Design
GATE CS
Mathematical
Adobe
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cache Memory in Computer Organization
Addressing Modes
Control Characters
Direct Access Media (DMA) Controller in Computer Architecture
Logical and Physical Address in Operating System
IEEE Standard 754 Floating Point Numbers
Difference between RAM and ROM
Introduction to memory and memory units
Difference between Half adder and full adder
Analog to Digital Conversion