Answer to Question #94217 in C++ for silas

Question #94217
Write a program to convert a number from a binary representation to a decimal format—that is, from base 2 to base 10. Your program should make use of recursion to do the conversion.
1
Expert's answer
2019-09-11T05:25:16-0400
#include <iostream>
#include <math.h>
using namespace std;
int bin_to_dec(long int binary, int radix, int decimal)
{
   if(binary%10)
   {
       decimal += pow(2, radix);
       binary = (binary - 1) / 10;
       radix++;
       bin_to_dec(binary, radix, decimal);
   }
   else if(binary != 0)
   {
       binary /= 10;
       radix++;
       bin_to_dec(binary, radix, decimal);
   }
   if(binary == 0)
       return decimal;
}
int main()
{
   cout<<bin_to_dec(1101001011, 0, 0)<<endl;//enter the number you ant to convert as the first argument
   return 0;
}

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
New on Blog
APPROVED BY CLIENTS