Advertisement
avr39-ripe

binTodecSimpleForDebug

Dec 18th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.33 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     int bin{ 0 };
  6.     int dec{ 0 };
  7.     std::cin >> bin;
  8.  
  9.     for (int mul{ 1 }, bit{ 0 }; bin; mul *= 2, bin /= 10)
  10.     {
  11.         bit = bin % 10;
  12.         if (bit != 0 and bit != 1)
  13.         {
  14.             std::cout << "Bad number!\n";
  15.             return 1;
  16.         }
  17.         dec += bit * mul;
  18.     }
  19.  
  20.     std::cout << "Decimal number: " << dec << '\n';
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement