Guest User

Untitled

a guest
Dec 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. char int_to_char (int n) {
  6.     char c = '0' + n;
  7.     return c;
  8. }
  9.  
  10. string dec_to_bin (int n) {
  11.     string s;
  12.     if (n < 2) {
  13.         s = int_to_char(n);
  14.         return s;
  15.     }
  16.     else {
  17.         s = int_to_char(n%2);
  18.         return dec_to_bin(n/2) + s;
  19.     }
  20. }
  21.  
  22. string dec_to_oct (int n) {
  23.     string s;
  24.     if (n < 8) {
  25.         s = int_to_char(n);
  26.         return s;
  27.     }
  28.     else {
  29.         s = int_to_char(n%8);
  30.         return dec_to_oct(n/8) + s;
  31.     }
  32. }
  33.  
  34. char int_to_hex (int n) {
  35.     if (n < 10) return '0' + n;
  36.     else return 'A' + n - 10;
  37. }
  38.  
  39. string dec_to_hex (int n) {
  40.     string s;
  41.     if (n < 16) {
  42.         s = int_to_hex(n);
  43.         return s;
  44.     }
  45.     else {
  46.         return  dec_to_hex (n/16) + int_to_hex (n%16);
  47.     }
  48. }
  49.  
  50. int main() {
  51.     int n;
  52.     while (cin >> n) {
  53.         cout << n << " = " << dec_to_bin(n) << ", " << dec_to_oct(n) << ", " << dec_to_hex (n) << endl;
  54.     }
  55. }
Add Comment
Please, Sign In to add comment