fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. int main() {
  5. long long num;
  6. int base;
  7.  
  8. printf("Enter the number to convert: ");
  9. scanf("%lld", &num);
  10.  
  11. printf("Enter the base of the number (e.g., 2 for binary, 8 for octal): ");
  12. scanf("%d", &base);
  13.  
  14. long long decimalValue = 0;
  15. long long power = 1;
  16.  
  17. bool isNegative = false;
  18. if (num < 0) {
  19. isNegative = true;
  20. num = -num;
  21. }
  22.  
  23. while (num > 0) {
  24. int digit = num % 10;
  25.  
  26. if (digit >= base) {
  27. printf("Error: Invalid digit '%d' for base %d\n", digit, base);
  28. return 1;
  29. }
  30.  
  31. decimalValue += digit * power;
  32. power *= base;
  33. num /= 10;
  34. }
  35.  
  36. if (isNegative) {
  37. decimalValue = -decimalValue;
  38. }
  39.  
  40. printf("The decimal equivalent is: %lld\n", decimalValue);
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter the number to convert: Enter the base of the number (e.g., 2 for binary, 8 for octal): The decimal equivalent is: 7812738726200174569