We can write this program (to convert an integer number represented as string to BCD code) in C++ as;
Integer number (string) to BCD code using C++
#include <iostream>
#include <string>
using namespace std;
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
int base = 1; // Initializing base value to 1, i.e 2^0
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}