Advertisement
Josif_tepe

Untitled

Feb 24th, 2022
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6. string print_binary(int x) {
  7.     string s = "";
  8.     while(x > 0) {
  9.         s += (x % 2) + '0';
  10.         x /= 2;
  11.     }
  12.     reverse(s.begin(), s.end());
  13.     return s;
  14. }
  15. int print_decimal(string s) {
  16.     int number = 0;
  17.     int power_of_two = 1;
  18.     for(int i = (int) s.size() - 1; i >= 0; i--) {
  19.         if(s[i] == '1') {
  20.             number += power_of_two;
  21.         }
  22.         power_of_two *= 2;
  23.     }
  24.     return number;
  25. }
  26. int main() {
  27.     int n;
  28.     cin >> n;
  29.     cout << print_binary(n) << endl;
  30.     cout << print_decimal(print_binary(n)) << endl;
  31.  
  32.     return 0;
  33. }
  34. // 1011000101110
  35. // 1011000101110
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement