Binary to Decimal Converter in C++

This program converts a binary number (entered as a string) to its decimal equivalent. Below is the complete C++ program, along with an explanation of its structure and functionality.

Program Code


#include <iostream>
#include <string>
#include <cmath>

// Function to convert binary to decimal
int binaryToDecimal(const std::string &binaryString) {
int decimalValue = 0;
int length = binaryString.length();

// Iterate through each character in the binary string
for (int i = 0; i < length; ++i) {
// Calculate the power of 2 for the current bit
int bit = binaryString[length - 1 - i] - '0';
decimalValue += bit * std::pow(2, i);
}

return decimalValue;
}

int main() {
std::string binaryString;

// Prompt user for input
std::cout << "Enter a binary number: ";
std::cin >> binaryString;

// Convert binary to decimal
int decimalValue = binaryToDecimal(binaryString);

// Display the result
std::cout << "The decimal equivalent is: " << decimalValue << std::endl;

return 0;
}

Explanation

The program is structured as follows:

1. Include Necessary Headers

The program includes the following headers:

  • #include <iostream> – For input and output operations.
  • #include <string> – To use the std::string class.
  • #include <cmath> – To use the std::pow function for power calculations.

2. Define the binaryToDecimal Function

This function takes a binary number as a string and converts it to its decimal equivalent. It iterates through each bit of the binary string, calculates its corresponding power of 2, and adds the result to the decimal value.

3. Main Function

The main function is the entry point of the program. It:

  • Prompts the user to enter a binary number.
  • Calls the binaryToDecimal function to convert the input to a decimal number.
  • Displays the decimal equivalent.

Running the Program

To run this program, you need a C++ compiler. Save the code in a file with a .cpp extension and compile it using a C++ compiler. For example, using g++:


g++ -o binary_to_decimal binary_to_decimal.cpp
./binary_to_decimal

Then enter a binary number when prompted to see its decimal equivalent.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)