Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://pastebin.com/u/TheWhiteFang
- //Tutorial 4 Section B
- //credits to sasi :P
- #include <iostream>
- #include <string>
- using namespace std;
- string DecToBin(int num){
- if(num==0) return "0"; //recursive call
- if(num==1) return "1";
- if(num %2 == 0) //rec base
- return DecToBin(num /2) + "0";
- else
- return DecToBin(num/2) + "1";
- }
- int main(){
- int num = 0;
- cout <<"Enter any positive integer number ";
- cin >> num;
- string ans = DecToBin(num);
- cout << "Binary of " << num << "is: " << ans <<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement