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