inbuilt function for bin to dec in c++

The C++ standard library does not provide a built-in function to convert a binary number to a decimal number. However, you can write a custom function to perform this conversion. Here's an example of how you can do it:

#include <iostream>
#include <string>

int binaryToDecimal(const std::string& binary) {
    int decimal = 0;
    int power = 1;

    for (int i = binary.length() - 1; i >= 0; i--) {
        if (binary[i] == '1') {
            decimal += power;
        }
        power *= 2;
    }

    return decimal;
}

int main() {
    std::string binaryNumber = "101010";
    int decimalNumber = binaryToDecimal(binaryNumber);

    std::cout << "Binary number: " << binaryNumber << std::endl;
    std::cout << "Decimal number: " << decimalNumber << std::endl;

    return 0;
}

In the binaryToDecimal function, we initialize the decimal variable to 0 and the power variable to 1. We iterate through the binary string from right to left using a for loop. For each digit in the binary string, if it is '1', we add the corresponding power of 2 to the decimal variable. The power of 2 is calculated using the power variable, which is multiplied by 2 in each iteration. Finally, we return the decimal variable, which represents the decimal equivalent of the binary number.

In the main function, we define a binary number as a string and call the binaryToDecimal function to convert it to decimal. We then print both the binary and decimal numbers using std::cout.

This code will output:

Binary number: 101010
Decimal number: 42

This demonstrates how you can convert a binary number to a decimal number in C++ using a custom function.