Advertisement
MeehoweCK

Untitled

Mar 2nd, 2021
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. string odwroc(string liczba)
  7. {
  8.     string wynik = "";
  9.     for(int i = liczba.size() - 1; i >= 0; --i)
  10.         wynik += liczba[i];
  11.     return wynik;
  12. }
  13.  
  14. int konwertuj_2_na_10(string liczba2)
  15. {
  16.     liczba2 = odwroc(liczba2);
  17.     unsigned n = liczba2.size();
  18.     int wynik = 0;
  19.     for(unsigned i = 0; i < n; ++i)
  20.         if(liczba2[i] == '1')
  21.             wynik += pow(2,i);
  22.     return wynik;
  23. }
  24.  
  25. int main()
  26. {
  27.     cout << konwertuj_2_na_10("10110");
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement