fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int convert_to_decimal (long long num, int base) {
  5. int decimal_value = 0;
  6. int power = 1;
  7.  
  8. while (num > 0) {
  9. int remainder = num % 10;
  10. num /= 10;
  11.  
  12. if (remainder >= base) {
  13. std::cout << "Error: Invalid digit for the given base" << std::endl;
  14. return -1;
  15. }
  16.  
  17. decimal_value += remainder * power;
  18. power *= base;
  19. }
  20. return decimal_value;
  21. }
  22.  
  23. int main() {
  24. long long number_in_other_base;
  25. int base;
  26.  
  27. std::cout <<"Enter the number in another base (e.g., 1011 for binary): ";
  28. std::cin >> base;
  29.  
  30. int result = convert_to_decimal(number_in_other_base, base);
  31.  
  32. if (result != -1) {
  33. std::cout << number_in_other_base << " in base " << base << " is "
  34. << result << " in decimal." << std::endl;
  35. }
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter the number in another base (e.g., 1011 for binary): 0 in base 32765 is 0 in decimal.