C++ Program to Convert Binary to Decimal

Problem Description

Write a C++ program that converts a given binary number into its decimal equivalent.

What is Binary Number?

A binary number is a number expressed using only two digits: 0 and 1. It is commonly used for computer operations and data representation.

What is Decimal Number?

A decimal number is a number represented using the base-10 numeral system, with digits ranging from 0 to 9. It is the standard way humans express numbers for everyday calculations.

Problem Solution

1. The program takes a binary number.
2. Using a while loop, the remainders of the number are multiplied with powers of 2.
3. The decimal equivalent that is the result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Convert a Given Binary Number to its Decimal Equivalent. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num, rem, temp, dec = 0, b = 1;
  6.     cout << "Enter the binary number : ";
  7.     cin >> num;
  8.     temp = num;
  9.     while (num > 0)
  10.     {
  11.         rem = temp % 10;
  12.         dec = dec + rem * b;
  13.         b *= 2;
  14.         temp /= 10;
  15.     }
  16.     cout << "The decimal equivalent of " << num << " is " << dec;
  17.     return 0;
  18. }
Example:

Input:
Enter the binary number: 1111

advertisement

Step by Step Process:

  1. Initialize num = 1111, temp = 1111, dec = 0, and b = 1.
  2. In the first iteration:
    • rem = 1111 % 10 = 1 (last digit of the binary number)
    • dec = 0 + 1 * 1 = 1
    • b = 1 * 2 = 2
    • temp = 1111 / 10 = 111 (removing the last digit)
  3. In the second iteration:
    • rem = 111 % 10 = 1 (next digit of the binary number)
    • dec = 1 + 1 * 2 = 3
    • b = 2 * 2 = 4
    • temp = 11 / 10 = 11
  4. In the third iteration:
    • rem = 11 % 10 = 1 (next digit of the binary number)
    • dec = 3 + 1 * 4 = 7
    • b = 4 * 2 = 8
    • temp = 1 / 10 = 1
  5. In the fourth iteration:
    • rem = 1 % 10 = 1 (last digit of the binary number)
    • dec = 7 + 1 * 8 = 15
    • b = 8 * 2 = 16
    • temp = 1 / 10 = 0
  6. The loop terminates as temp becomes 0.

Output:
The decimal equivalent of 1111 is 15.

Program Explanation

1. The user is asked to enter a binary number and it is stored in the variable ‘num’.
2. The variable ‘b’ is initialized as 1 and ‘dec’ as 0. num is copied to the variable ‘temp’.
3. Using a while loop, modulus of temp is stored in ‘rem’.
4. rem is multiplied with b and added to dec.
5. In every iteration, b is multiplied with 2.
6. The loop continues till temp is less than 0.
7. The decimal equivalent is stored in dec and printed.

Time Complexity: O(log n)
The time complexity of this code is O(log n), where n is the number of digits in the binary number. The while loop iterates through the digits of the binary number, and the number of iterations is proportional to the number of digits in the binary number.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

Space Complexity: O(1)
The space complexity of this code is O(1). It uses a fixed number of variables regardless of the input size. Hence, the space required remains constant, making it O(1).

Runtime Test Cases

Testcase 1: In this case, we are entering the binary number “10” as input.

Enter the binary number : 10
The decimal equivalent of 10 is 2

Testcase 2: In this case, we are entering the binary number “01010101” as input.

Enter the binary number : 01010101
The decimal equivalent of 1010101 is 85

Testcase 1: In this case, we are entering the binary number “1111” as input.

Enter the binary number : 1111
The decimal equivalent of 1111 is 15

To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.