Write a C++ program that converts a given binary number into its decimal equivalent.
A binary number is a number expressed using only two digits: 0 and 1. It is commonly used for computer operations and data representation.
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.
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.
Here is the source code of C++ Program to Convert a Given Binary Number to its Decimal Equivalent. The program output is shown below.
#include<iostream>
using namespace std;
int main ()
{
int num, rem, temp, dec = 0, b = 1;
cout << "Enter the binary number : ";
cin >> num;
temp = num;
while (num > 0)
{
rem = temp % 10;
dec = dec + rem * b;
b *= 2;
temp /= 10;
}
cout << "The decimal equivalent of " << num << " is " << dec;
return 0;
}
Input:
Enter the binary number: 1111
Step by Step Process:
- Initialize num = 1111, temp = 1111, dec = 0, and b = 1.
- 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)
- 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
- 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
- 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
- The loop terminates as temp becomes 0.
Output:
The decimal equivalent of 1111 is 15.
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.
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).
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++”.
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Programming Books