Answer to Question #39739 in C++ for Ayeza

Question #39739
a code on how to convert binary to decimal in c programming.
1
Expert's answer
2014-03-13T13:30:04-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS