daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Dec 18th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. char int_to_char (int n) {
  6.     if (n < 10) return '0' + n;
  7.     else return 'A' + n - 10;
  8. }
  9.  
  10. string change_base (int n, int b) {
  11.     string s;
  12.     if (n < b) {
  13.         s = int_to_char(n);
  14.         return s;
  15.     }
  16.     else {
  17.         s = int_to_char(n%b);
  18.         return change_base(n/b, b) + s;
  19.     }
  20. }
  21.  
  22. int main() {
  23.     int n;
  24.     while (cin >> n) {
  25.         cout << n << " = " << change_base(n, 2) << ", " << change_base(n, 8) << ", " << change_base(n, 16) << endl;
  26.     }
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top