Convert an inputted binary number to a decimal number. code example
Example 1: convert binary to decimal c++ stl
string bin_string = "10101010";
int number =0;
number = stoi(bin_string, 0, 2);
// number = 170
Example 2: binary to decimal in c
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}