fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int binaryToDecimal(string binary) {
  9. int decimal = 0;
  10. int len = binary.length();
  11. for (int i = 0; i < len; i++) {
  12. if (binary[len - 1 - i] == '1') {
  13. decimal += pow(2, i);
  14. }
  15. }
  16. return decimal;
  17. }
  18. int main() {
  19. string n1 = "101010";
  20. string n2 = "110101";
  21.  
  22. cout << n1 << " w systemie dziesiętnym to: " << binaryToDecimal(n1) << endl;
  23. cout << n2 << " w systemie dziesiętnym to: " << binaryToDecimal(n2) << endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
101010 w systemie dziesiętnym to: 42
110101 w systemie dziesiętnym to: 53