Xom9ik

Alg_Lab_8 (lll semester)

Nov 14th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "windows.h"
  4.  
  5. using namespace std;
  6.  
  7. void dec_to_bin(int dec)
  8. {
  9.     if (dec >= 2)
  10.         dec_to_bin(dec / 2);
  11.     cout << dec % 2;
  12. }
  13. void dec_to_oct(int dec)
  14. {
  15.     if (dec >= 8)
  16.         dec_to_oct(dec / 8);
  17.     cout << dec % 8;
  18. }
  19. void dec_to_hex(int dec)
  20. {
  21.     if (dec == 0)
  22.         return;
  23.  
  24.     int rem = dec % 16;
  25.     dec /= 16;
  26.     dec_to_hex(dec);
  27.     if (rem > 9)
  28.         cout << (char)(rem - 10 + 'A');
  29.     else
  30.         cout << rem;
  31. }
  32. int main()
  33. {
  34.     while (1)
  35.     {
  36.         int dec;
  37.         cout << "Enter value: ";
  38.         cin >> dec;
  39.         cout << dec << "[10] -> ";
  40.         dec_to_bin(dec);
  41.         cout << "[2]" << endl;
  42.         cout << dec << "[10] -> ";
  43.         dec_to_oct(dec);
  44.         cout << "[8]" << endl;
  45.         cout << dec << "[10] -> ";
  46.         dec_to_hex(dec);
  47.         cout << "[16]" << endl << endl;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment