# Answer to Question #39739 in C++ for Ayeza

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;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment