Answer to Question #92915 in C++ for Mokete

Question #92915
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-08-20T05:28:06-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(1101001, 0, 0)<<endl;
   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