Question

In: Computer Science

c ++ program that converts from any base to a decimal number

c ++ program that converts from any base to a decimal number

Solutions

Expert Solution

C++ program : (Converts from any base to a decimal number)

#include <iostream>
using namespace std;
/*
To find decimal equivalent of a number N in any base we have the formula , 
let n be the length of number then, (Here ^ represents power symbol)
Decimal Equivalent = ((base)^0 )*N[n-1] + ((base)^1) * N[n-2] + ((base)^2) * N[n-3] + ....
*/
int main()
{
    string num;
    int base;
    cout << "Please enter a number: ";
    cin >> num;
    cout << "Please enter the base of the number: ";
    cin >> base;
    
    int n = num.length();  // n stores length of number
    int power = 1;
    int result = 0, digit, flag = 1; // decimal value  is stored in result
    for(int i = n-1; i>=0; i--)      // iterate through all digits in number
    {
        if(num[i]>='0' && num[i]<='9')  // convert characters to integers
        {
            digit = (int)num[i] - '0';
        }
        else                           // convert characters to integers , Here 'A' = 10, 'B' = 11 ... 'Z' = 36
        {
            digit = (int)num[i] - 'A' + 10;
        }
        
        if(digit >=base)              // if digit is greater than base then its invalid number for the given base
        {
            flag = 0;
            cout << "You have entered a invalid number for the given base\n";
            break;
        }
        result += digit*power;       // calculate result
        power = power*base;          // multiply power with base
    }
    if(flag) // if flag is set to 1 then we get decimal equivalent for valid number for that base
    {
        cout<< "Decimal equivalent of "<<num<<" in base "<<base<<" is "<<result<<"\n";
    }

    return 0;
}

Screenshots and outputs :


Related Solutions

Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Make a simple C++ program not complex Any number is entered from keyboard of base 2,...
Make a simple C++ program not complex Any number is entered from keyboard of base 2, base 8, base 10 or base 16. Program asks for conversion options (menu) to convert into required base, i.e., 2, 8, 10 or 16. User selects the option and program converts the input number to the selected base number. Output number is shown on the screen. Following points are to be considered while making the program: It should be a menu driven program, use...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your...
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, Roman. An object of type Roman should do the following: Store the number as a Roman numeral. Convert and store the number into decimal. Print the number as a Roman numeral or decimal number as requested by the user. The decimal values of the Roman numerals are: M 1000 D 500 C 100 L 50 X 10...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT