Program for Binary To Decimal Conversion
Given a binary number as input, we need to write a program to convert the given binary number into equivalent decimal number.
Examples :
Input : 111 Output : 7 Input : 1010 Output : 10 Input: 100001 Output: 33
The idea is to extract the digits of given binary number starting from right most 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. At 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
Below diagram explains how to convert ( 1010 ) to equivalent decimal value:

Below is the implementation of above idea :
C++
// C++ program to convert binary to decimal #include <iostream> using namespace std; // Function to convert binary to decimal 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) { int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } // Driver program to test above function int main() { int num = 10101001; cout << binaryToDecimal(num) << endl; } |
Java
// Java program to convert // binary to decimal // Function to convert // binary to decimal class 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
# Python3 program to convert # binary to decimal # Function to convert # binary to decimal def 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 Code num = 10101001; print(binaryToDecimal(num)); # This code is contributed by mits |
C#
// C# program to convert // binary to decimal // Function to convert // binary to decimal class 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 // PHP program to convert // binary to decimal // Function to convert // binary to decimal function 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 ?> |
Output:
169
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 string variable to store the binary numbers.
Below is a similar program which uses string variable instead of integers to store binary value:
C++
// 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 decimal 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[i] == '1') dec_value += base; base = base * 2; } return dec_value; } // Driver program to test above function int main() { string num = "10101001"; cout << binaryToDecimal(num) << endl; } |
Java
// 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
# Python3 program to convert binary # to decimal when input is # represented as binary string. # Function to convert # binary to decimal def 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 Code num = "10101001"; print(binaryToDecimal(num)); # This code is contributed by mits |
C#
// 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 // PHP program to convert binary // to decimal when input is // represented as binary string. // Function to convert // binary to decimal function 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 ?> |
Output :
169
This can also be done by Integer.parseInt() function in Java.
Java
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 |
Output :
9
This article is contributed by Harsh Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Program for Decimal to Binary Conversion
- Decimal to binary conversion without using arithmetic operators
- Program for decimal to octal conversion
- Program for decimal to hexadecimal conversion
- Program for octal to decimal conversion
- Recursive Program for Binary to Decimal
- Python program to covert decimal to binary number
- 8086 program to convert a 16 bit decimal number to binary
- Decimal to octal conversion with minimum use of arithmetic operators
- Convert Binary fraction to Decimal
- Decimal to binary number using recursion
- Decimal representation of given binary string is divisible by 20 or not
- Binary to decimal and vice-versa in python
- Decimal to Binary using recursion and without using power operator
- Decimal representation of given binary string is divisible by 5 or not




