Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C++ Programs to Convert Binary to Decimal Number

Last Updated on April 13, 2023 by Prepbytes

The binary is a base-2 numbering system that uses only two digits, 0 and 1, to represent numbers. Decimal is a base-10 numbering system that uses ten digits, 0 through 9, to represent numbers. Convert binary to decimal c++ involves multiplying each digit of the binary number by the corresponding power of 2 and adding the results.

Binary Number

A binary number is defined as a number that is expressed in the binary system or base 2 numeral system. It describes numeric values by two separate symbols; 1 (one) and 0 (zero).

Decimal Number

Decimal is a term that describes the base-10 number system, probably the most commonly used number system. The decimal number system consists of ten single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The number after 9 is 10. The number after 19 is 20 and so forth.

Example:

Input1: 111
Output1: 7
Explanation: (122) + (121)+(1*20) = 4 + 2 + 1 = 7

Input2: 1010
Output2: 10

Input3: 110
Output3: 6

How to Convert Binary to Decimal in C++ using Recursion

Steps to convert binary to decimal c++ using recursion:

  1. Take the input string strBinary[] containing a binary number.
  2. Calculate its length using strlen(strBinary).
  3. Function BinarytoDecimal(strBinary, length) takes input and returns the number calculated using a recursive approach.
  4. If we are at the last character which is LSB, then return its decimal as it will be the same. (multiplied by 1 i.e 20 )
  5. Otherwise set temp=binary[i] – ‘0’. It’s a decimal value.
  6. Now multiply temp with 2len-i-1 using temp << len – i – 1.
  7. Add the result of other digits to temp using temp = temp + BinarytoDecimal(binary, len, i + 1).
  8. At the end of the recursion return temp.
  9. Print the calculated decimal in the main.

For Example:
If the binary number is 111.
dec_value = 1(22) + 1(21) + 1(20) = 7
The below diagram explains how to convert ( 1010 ) to an equivalent decimal value:

Code Implementation of Recursive Approach for converting Binary Number into Decimal Number in C++

#include<bits/stdc++.h>
using namespace std;
int BinarytoDecimal(char binary[],int len, int i=0){
   if (i == len-1)
   return (binary[i] - '0');

   int temp=binary[i]-'0';
   temp=temp<<len-i-1;
   temp=temp+BinarytoDecimal(binary,len,i+1);
   return (temp);
}
int main(){
   char strBinary[] = "111";
   int length=strlen(strBinary);
   cout <<"Decimal Number of given binary number: "<<BinarytoDecimal(strBinary,length) << endl;
   return 0;
}

Time Complexity: The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

Space Complexity: The space complexity of this implementation of converting a binary number into a decimal number is O(1).

Function to Convert Binary to Decimal in C++ using STL

For the function to convert binary to decimal in c++ using STL, we will use stoi inbuilt function. In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.

To use stoi, you’ll need to provide specific criteria for the function:

stoi(string, position, int base):

The first criterion is the string that needs to be converted.
Next, we’ll need to specify the identifier for the starting position of the number within the string that needs to be parsed.
int base defines the numerical base for the string. As examples, we have 2 for binary, 16 for hexadecimal, and 10 for base 10.
Stoi function will directly convert binary strings to decimal numbers.

Code Implementation using Predefined Functions of C++ for the conversion of Binary Number into Decimal Number

#include <iostream>
using namespace std;

int main()
{
	char binaryNumber[] = "1010";
	
	cout << stoi(binaryNumber, 0, 2);

	return 0;
}

Time Complexity: The time complexity of this implementation of converting binary numbers into decimal numbers is O(N) where N is the length of the string.

Space Complexity: The space complexity of this implementation of converting a binary number into a decimal number is O(1).

How to Convert Binary to Decimal in C++ using For Loop

Steps to convert binary to decimal c++ using for loop:

  1. Firstly, initialize the decimal_num variable with the value 0, add the binary number in the bin_num variable, and initialize rem.
  2. Now, start a for loop with condition i = 0, bin_num != 0, ++i.
  3. In the loop block,
    • Update the rem value to bin_num % 10.
    • Change the value of bin_num with bin_num / 10.
    • Atlast change the value of decimal_num with decimal_num + (rem) * (pow(2, i)).
  4. At the end of the for loop, print the decimal_num variable which is having the value of the decimal number of the given binary number.

Code Implementation of For Loop approach for converting Binary Number into Decimal Number in C++

#include<bits/stdc++.h>
using namespace std;

int main() {
	int i, bin_num, decimal_num = 0, rem;  
    bin_num = 111;
    for (i = 0; bin_num != 0; ++i)  
    {  
        rem = bin_num % 10;  
        bin_num = bin_num / 10;  
        decimal_num = decimal_num + (rem) * ( pow (2, i));  
 
    }  
    cout<<"\n Decimal Number of given binary number:"<<decimal_num;
	return 0;
}

Time Complexity: The time complexity of this implementation of converting binary numbers into decimal numbers is O(logN).

Space Complexity: The space complexity of this implementation of converting binary numbers into decimal numbers is O(1).

Conclusion
In conclusion, convert binary to decimal c++ involves multiplying each bit with the corresponding power of 2 and adding up the results. The position of each bit represents a power of 2, with the rightmost bit representing 2^0. Decimal numbers can also be converted to binary numbers by repeatedly dividing the decimal number by 2 and recording the remainder. The largest binary number that can be converted to a decimal number using 8 bits is 255, and the smallest is 0.

Frequently Asked Questions(FAQs)

Q1. What is a binary number system?
Ans: The binary number system is a base-2 number system, which uses only two digits, 0 and 1, to represent all the numbers. Each digit in the binary number system is called a bit.

Q2. What is the decimal number system?
Ans: A decimal number system is a base-10 number system, which uses 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) to represent all the numbers. Each digit in the decimal number system represents a power of 10.

Q3. How to convert binary to decimal c++?
Ans: To convert a binary number to a decimal number, multiply each bit of the binary number with the corresponding power of 2 and add up the results. For example, to convert the binary number 1101 to decimal, you would calculate: 1 2^3 + 1 2^2 + 0 2^1 + 1 2^0 = 13.

Q4. What is the significance of the position of each bit in the binary number system?
Ans: The position of each bit in the binary number system represents a power of 2. The rightmost bit represents 2^0, the next bit represents 2^1, the next bit represents 2^2, and so on. Each successive bit represents a power of 2 that is double the previous power.

Q5. Can decimal numbers be converted to binary numbers?
Ans: Yes, decimal numbers can be converted to binary numbers. To convert a decimal number to a binary number, you need to repeatedly divide the decimal number by 2 and record the remainder. The binary number is then obtained by writing the remainder in reverse order.

Q6. What is the largest binary number that can be converted to a decimal number using 8 bits?
Ans: The largest binary number that can be converted to a decimal number using 8 bits is 255. This is because the largest 8-bit binary number is 11111111, which represents 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 255 in decimal.

Q7. What is the smallest binary number that can be converted to a decimal number using 8 bits?
Ans: The smallest binary number that can be converted to a decimal number using 8 bits is 0. This is because the smallest 8-bit binary number is 00000000, which represents 0 in decimal.

Leave a Reply

Your email address will not be published. Required fields are marked *