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.
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...
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...
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT