Posted on by Kalkicode
Code Conversion

# Convert Binary to Decimal number

Binary is a number system that uses only two digits, 0 and 1, to represent numbers. In contrast, decimal is the number system that we use in everyday life, which uses ten digits, 0 through 9, to represent numbers.

To convert a binary number to a decimal number, you need to follow these steps:

1. Write down the binary number.

2. Assign a place value to each digit in the binary number, starting with 1 for the rightmost digit, and doubling the place value for each digit to the left.

3. Multiply each digit in the binary number by its corresponding place value.

4. Add up the results of step 3 to get the decimal equivalent of the binary number.

For example, to convert the binary number 1010111 to decimal, you would:

1. Write down the binary number: 1010111

2. Assign place values: 1, 2, 4, 8, 16,32,64 (starting from the right)

3. Multiply each digit by its corresponding place value: 1 x 1 + 1 x 2 + 1 x 4 + 0 x 8 + 1 x 16 + 0 x 32 + 1 x 64 = 87

4. The decimal equivalent of 1010111 in binary is 87.

## Program Solution

When the given binary number is in the form of a decimal number, We can convert the decimal to a string. And another way.

``````// C Program
// Convert Binary to Decimal number
#include <stdio.h>

void binary_to_decimal(long long number)
{
// Used to store result
long long result = 0;
int counter = 0;
// Display Binary number
printf("Binary : %lld ", number);
while (number != 0)
{
if ((number & 1) == 0b1)
{
// When get 1 in form of binary bit
result += (number % 10) *(1 << (counter));
}
counter++;
number /= 10;
}
// Display decimal result
printf(" Decimal : %lld \n", result);
}
int main()
{
// Pass a decimal number which is contain binary values
binary_to_decimal(1111);
binary_to_decimal(10111);
binary_to_decimal(10101);
binary_to_decimal(1001);
binary_to_decimal(1101);
return 0;
}``````

#### Output

``````Binary : 1111  Decimal : 15
Binary : 10111  Decimal : 23
Binary : 10101  Decimal : 21
Binary : 1001  Decimal : 9
Binary : 1101  Decimal : 13``````
``````/*
Java Program
Convert Binary to Decimal number
*/
public class DecimalNumber
{
public void binaryTodecimal(long number)
{
// Used to store result
long result = 0;
int counter = 0;
// Display Binary number
System.out.print("Binary :  " + number);
// Execute loop when number is greater than zero
while (number != 0)
{
if ((number & 1) == 0b1)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter));
}
counter++;
number /= 10;
}
// Display decimal result
System.out.println("  Decimal :  " + result );
}
public static void main(String[] args)
{
// Test Cases
// Pass a decimal number which is contain binary values
}
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Convert Binary to Decimal number
*/
class DecimalNumber
{
public: void binaryTodecimal(long number)
{
// Used to store result
long result = 0;
int counter = 0;
// Display Binary number
cout << "Binary :  " << number;
// Execute loop when number is greater than zero
while (number != 0)
{
if ((number & 1) == 1)
{
// When get 1 in form of binary bit
result += (number % 10) *(1 << (counter));
}
counter++;
number /= 10;
}
// Display decimal result
cout << "  Decimal :  " << result << endl;
}
};
int main()
{
// Test Cases
// Pass a decimal number which is contain binary values
return 0;
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````package main
import "fmt"
/*
Go Program
Convert Binary to Decimal number
*/
type DecimalNumber struct {}
func getDecimalNumber() * DecimalNumber {
var me *DecimalNumber = &DecimalNumber {}
return me
}
func(this DecimalNumber) binaryTodecimal(number int64) {
// Used to store result
var result int64 = 0
var counter int = 0
// Display Binary number
fmt.Print("Binary :  ", number)
// Execute loop when number is greater than zero
for (number != 0) {
if (number & 1) == 1 {
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter))
}
counter++
number /= 10
}
// Display decimal result
fmt.Println("  Decimal :  ", result)
}
func main() {
var task * DecimalNumber = getDecimalNumber()
// Test Cases
// Pass a decimal number which is contain binary values
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````// Include namespace system
using System;
/*
Csharp Program
Convert Binary to Decimal number
*/
public class DecimalNumber
{
public void binaryTodecimal(long number)
{
// Used to store result
long result = 0;
int counter = 0;
// Display Binary number
Console.Write("Binary :  " + number);
// Execute loop when number is greater than zero
while (number != 0)
{
if ((number & 1) == 1)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter));
}
counter++;
number /= 10;
}
// Display decimal result
Console.WriteLine("  Decimal :  " + result);
}
public static void Main(String[] args)
{
// Test Cases
// Pass a decimal number which is contain binary values
}
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````<?php
/*
Php Program
Convert Binary to Decimal number
*/
class DecimalNumber
{
public	function binaryTodecimal(\$number)
{
// Used to store result
\$result = 0;
\$counter = 0;
// Display Binary number
echo("Binary :  ".\$number);
// Execute loop when number is greater than zero
while (\$number != 0)
{
if ((\$number & 1) == 1)
{
// When get 1 in form of binary bit
\$result += (\$number % 10) * (1 << (\$counter));
}
\$counter++;
\$number = \$number / 10;
}
// Display decimal result
echo("  Decimal :  ".\$result."\n");
}
}

function main()
{
// Test Cases
// Pass a decimal number which is contain binary values
}
main();``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````/*
Node JS Program
Convert Binary to Decimal number
*/
class DecimalNumber
{
binaryTodecimal(number)
{
// Used to store result
var result = 0;
var counter = 0;
// Display Binary number
process.stdout.write("Binary :  " + number);
// Execute loop when number is greater than zero
while (number != 0)
{
if ((number & 1) == 1)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter));
}
counter++;
number = parseInt(number / 10);
}
// Display decimal result
console.log("  Decimal :  " + result);
}
}

function main()
{
// Test Cases
// Pass a decimal number which is contain binary values
}
main();``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````#  Python 3 Program
#  Convert Binary to Decimal number
class DecimalNumber :
def binaryTodecimal(self, number) :
#  Used to store result
result = 0
counter = 0
#  Display Binary number
print("Binary :  ", number, end = "")
#  Execute loop when number is greater than zero
while (number != 0) :
if ((number & 1) == 1) :
#  When get 1 in form of binary bit
result += (number % 10) * (1 << (counter))

counter += 1
number = number // 10

#  Display decimal result
print("  Decimal :  ", result)

def main() :
#  Test Cases
#  Pass a decimal number which is contain binary values

if __name__ == "__main__": main()``````

#### Output

``````Binary :   1111  Decimal :   15
Binary :   10111  Decimal :   23
Binary :   10101  Decimal :   21
Binary :   1001  Decimal :   9
Binary :   1101  Decimal :   13``````
``````#  Ruby Program
#  Convert Binary to Decimal number
class DecimalNumber
def binaryTodecimal(number)
#  Used to store result
result = 0
counter = 0
#  Display Binary number
print("Binary :  ", number)
#  Execute loop when number is greater than zero
while (number != 0)
if ((number & 1) == 1)
#  When get 1 in form of binary bit
result += (number % 10) * (1 << (counter))
end

counter += 1
number = number / 10
end

#  Display decimal result
print("  Decimal :  ", result, "\n")
end

end

def main()
#  Test Cases
#  Pass a decimal number which is contain binary values
end

main()``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
``````
``````/*
Scala Program
Convert Binary to Decimal number
*/
class DecimalNumber()
{
def binaryTodecimal(num: Long): Unit = {
// Used to store result
var result: Long = 0;
var counter: Int = 0;
// Display Binary number
print("Binary :  " + num);
var number = num;
// Execute loop when number is greater than zero
while (number != 0)
{
if ((number & 1) == 1)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter));
}
counter += 1;
number = number / 10;
}
// Display decimal result
println("  Decimal :  " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DecimalNumber = new DecimalNumber();
// Test Cases
// Pass a decimal number which is contain binary values
}
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````
``````/*
Swift 4 Program
Convert Binary to Decimal number
*/
class DecimalNumber
{
func binaryTodecimal(_ num: Int)
{
// Used to store result
var result: Int = 0;
var counter: Int = 0;
var number: Int = num;
// Display Binary number
print("Binary :  ", number, terminator: "");
// Execute loop when number is greater than zero
while (number  != 0)
{
if ((number & 1) == 1)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 << (counter));
}
counter += 1;
number = number / 10;
}
// Display decimal result
print("  Decimal :  ", result);
}
}
func main()
{
// Test Cases
// Pass a decimal number which is contain binary values
}
main();``````

#### Output

``````Binary :   1111  Decimal :   15
Binary :   10111  Decimal :   23
Binary :   10101  Decimal :   21
Binary :   1001  Decimal :   9
Binary :   1101  Decimal :   13``````
``````/*
Kotlin Program
Convert Binary to Decimal number
*/
class DecimalNumber
{
fun binaryTodecimal(num: Long): Unit
{
// Used to store result
var result: Long = 0;
var counter: Int = 0;
var number: Long = num;
// Display Binary number
print("Binary :  " + number);
// Execute loop when number is greater than zero
while (number != 0L)
{
if ((number and 1) == 1L)
{
// When get 1 in form of binary bit
result += (number % 10) * (1 shl(counter));
}
counter += 1;
number = number / 10;
}
// Display decimal result
println("  Decimal :  " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test Cases
// Pass a decimal number which is contain binary values
}``````

#### Output

``````Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post