You are here:

C++/recursive function

Advertisement


Question
Would like to write program to convert decimal to binary using recursive function Can you help please?

Thanks

Answer
#include <iostream>

using namespace std;

void convert ( int dec )
{
   while ( dec != 0 ) {
       cout<< !( dec & 1 );
       dec >>= 1;
   }
   cout<<endl;
}

void convert_r ( int dec )
{
   if ( dec == 0 ) {
       cout<<endl;
       return;
   }
   cout<< !( dec & 1 );
   convert_r ( dec >> 1 );
}

int main()
{
   convert ( 'A' );
   convert_r ( 'A' );
}

-Sandra

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sandra Guth

Expertise

I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.

Experience

I have nine years of programming experience with C++.

©2016 About.com. All rights reserved.