fork download
  1. // C++ program to convert binary to decimal
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Function to convert binary to decimal
  6. int binaryToDecimal(int n)
  7. {
  8. int num = n;
  9. int dec_value = 0;
  10.  
  11. // Initializing base value to 1, i.e 2^0
  12. int base = 1;
  13.  
  14. int temp = num;
  15. while (temp) {
  16. int last_digit = temp % 10;
  17. temp = temp / 10;
  18.  
  19. dec_value += last_digit * base;
  20.  
  21. base = base * 2;
  22. }
  23.  
  24. return dec_value;
  25. }
  26.  
  27. // Driver program to test above function
  28. int main()
  29. {
  30. int num = 1011101;
  31.  
  32. cout << binaryToDecimal(num) << endl;
  33. }
  34.  
Success #stdin #stdout 0s 4180KB
stdin
Standard input is empty
stdout
93