Question #39739

a code on how to convert binary to decimal in c programming.

Expert's answer

#include <iostream>

int binaryToDecimal(int n)

{

int output = 0;

for(int i=0; n > 0; i++)

{

if (n % 10 == 1)

output += (1 << i);

n /= 10;

}

return output;

}

int main (void)

{

std::cout << "Enter binary number: ";

int binaryNumber;

std::cin >> binaryNumber;

std::cout << "Decimal: " << binaryToDecimal(binaryNumber) << std::endl;

}

int binaryToDecimal(int n)

{

int output = 0;

for(int i=0; n > 0; i++)

{

if (n % 10 == 1)

output += (1 << i);

n /= 10;

}

return output;

}

int main (void)

{

std::cout << "Enter binary number: ";

int binaryNumber;

std::cin >> binaryNumber;

std::cout << "Decimal: " << binaryToDecimal(binaryNumber) << std::endl;

}

## Comments

## Leave a comment