# Convert the given Binary Number into Decimal | faceprep

Program to convert the given binary number into  decimal number is discussed here. For example, the decimal equivalent of binary number 1111 is 15. Consider the binary number from the last.

For the above mentioned example,

1 * 2^0 = 1
1 * 2^1 = 2
1 * 2^2 = 4
1 * 2^3 = 6

Decimal number = 1 + 2 + 4 + 6 = 15

1111 in binary form is represented as 15 in decimal.

## Algorithm to convert the given binary number into decimal number

• Input the binary number.
• Multiply each digit of the binary number with the power of 2 and add each multiplication result.
• The power starts from 0 to n-1 where n is the total number of digits in the binary number.

## Program to convert the given binary number into decimal number

// C program to convert a binary number into decimal number

#include
#include

int binary_to_decimal(long int n)
{
int decimal = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimal += remainder*pow(2,i);
++i;
}
return decimal;
}

int main()
{
long int n;
printf(“Enter a binary number: “);
scanf(“%ld”, &n);
printf(“\nDecimal number : %d\n “, binary_to_decimal(n));
return 0;
}

// C++ program to convert a binary number into decimal number

#include
#include using namespace std;

int binary_to_decimal(long int n)
{
int decimal = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimal += remainder*pow(2,i);
++i;
}
return decimal;
}

int main()
{
long int n;
cout << “Enter a binary number: “;
cin >> n;
cout << “\nDecimal number : ” << binary_to_decimal(n) << endl;
return 0;
}

// Java program to convert abinary number into decimal number

import java.util.*;
public class binary_to_decimal {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the binary number : “);
int n = sc.nextInt();
int decimal = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimal += remainder*Math.pow(2,i);
++i;
}
System.out.println(“Decimal number : ” + decimal);
}
}

# Python program to convert a binary number into decimal number

import math
n = int(input(“Enter the binary number : “))
decimal = 0
remainder = 0
i = 0
while (n!=0):
remainder = n%10
n = int(n / 10)
decimal = decimal + remainder*math.pow(2,i)
i = i + 1
print(“Decimal Number : “,int(decimal))

Output: The above program works only with binary numbers given in the range of integers (-32767 to 32767). To work with long binary numbers (greater range of numbers with 20 bits or 30 bits), a  string variable is used to store the binary numbers.

Program to convert numbers from binary numbers of higher range to decimal is given below

// C++ program to convert binary to decimal
#include <iostream>
#include <string>
using namespace std;

// Function to convert binary to decimal
int binary_to_decimal(string n)
{
string num = n;
int decimal = 0;

// Initializing base value to 1 which is (2^0)
int base = 1;

int len = num.length();
for (int i = len – 1; i >= 0; i–) {
if (num[i] == ‘1’)
decimal+= base;
base = base * 2;
}

return decimal;
}

int main()
{
string num;
cout <<“\nEnter the binary number : “;
cin >> num;
cout << “\nDecimal Equivalent : ” << binary_to_decimal(num) << endl;
}

// Java program to convert binary to decimal

import java.io.*;

class Main {

// Function to convert binary to decimal
static int binaryToDecimal(String n)
{
String num = n;
int dec_value = 0;

// Initializing base value to 1 which is (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;
}

public static void main(String[] args)
{
String num = new String(“1111”);
System.out.println(binaryToDecimal(num));
}
}

# Python program to convert binary to decimal

def binaryToDecimal(n):
num = n;
dec_value = 0;

# Initializing base
# value to 1, which is 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;

num = “1111”;
print(binaryToDecimal(num));