Question

in C++, Program 4: Conversion between Bases Write a program that will convert a decimal number...

in C++, Program 4: Conversion between Bases Write a program that will convert a decimal number to binary, octal, and hexadecimal (base 2, 8, and 16). Program Requirements: Prompt the user to input an integer value in decimal (base 10) and display the value in base 2, 8 and 16. Do NOT use built-in conversion methods for the conversion. You must do your own math to compute the values. Repeat the prompt and conversion until the user enters a sentinel value to quit. For example: Input a decimal: 23 23 in base 2: 10111 23 in base 8: 27 23 in base 16: 17 Additional Requirements: Do not allow negative or non-numeric input values from the user. The program should automatically display the conversion to the 3 required bases, don't make the user choose a base to convert to.

Homework Answers

Answer #1

#include <iostream>
#include <sstream>
using namespace std;

void decimal_to_binary(int n)
{
int binary[32]; // binary array

cout << n << " in base 2 : ";

int i = 0; // counter for binary array
while (n > 0) {

binary[i] = n % 2; // storing the remainder in binary array
n = n / 2;
i++;
}

for (int j = i - 1; j >= 0; j--) // print binary array in reverse order
cout << binary[j];

cout << endl;
}
void decimal_to_octal(int n)
{

int octal[100]; // octal array

cout << n << " in base 8 : ";

int i = 0; // counter for octal array
while (n != 0) {

octal[i] = n % 8; // storing remainder in octal array
n = n / 8;
i++;
}

for (int j = i - 1; j >= 0; j--) // print octal array in reverse order
cout << octal[j];

cout << endl;
}

void decimal_to_hexa(int n)
{

char hexaDeci[100]; // hexadecimal array

cout << n << " in base 16 : ";

int i = 0; // counter for hexadecimal array
while(n!=0)
{

int temp = 0;

temp = n % 16; // storing remainder

if(temp < 10) // check if temp < 10
{
hexaDeci[i] = temp + 48; // it gives numeric digit
i++;
}
else
{
hexaDeci[i] = temp + 55; // it gives alphabetic character
i++;
}
n = n/16;
}

for(int j=i-1; j>=0; j--) // print hexadecimal number array in reverse order
cout << hexaDeci[j];

cout << endl;
}
int main()
{
string decimal; // take decimal value as a string
bool flag = true;

while(true)
{
cout << "Enter decimal (base 10) value : ";
cin >> decimal;

for (int i = 0; i < decimal.length(); i++)
if (isdigit(decimal[i]) == false) // check each element of string
flag = false;

if (!flag) // sentinel value
break;

// object from the class stringstream
stringstream convert(decimal);
int n = 0;
convert >> n; // stream integer value to n

decimal_to_binary(n);
decimal_to_octal(n);
decimal_to_hexa(n);

cout << endl;
}
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ 11, create a program does the following 4 conversions: From Binary to Decimal; From...
In C++ 11, create a program does the following 4 conversions: From Binary to Decimal; From Decimal to Binary; From Hexadecimal to Binary; From Binary to Hexadecimal; First create a menu so that user can choose which conversion they wanna do;
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your work. (a) 368 (b) 7568 3 Convert the following binary values into Decimal, Octal and hexadecimal. Show your work. (a) 111010101011112 (b) 1010111011001102 (c) 1011101010001112 (d) 1111101011102 4 Convert the following hexadecimal numbers to 16-bit binary and decimal numbers. Show your work. (a) FE9816 (b) FCAD16 (c) B00C16 (d) FEDF16 5 Perform the addition on the following unsigned binary numbers. Indicate whether or not...
In this lab we will design a menu-based program. The program will allow users to decide...
In this lab we will design a menu-based program. The program will allow users to decide if they want to convert a binary number to base 10 (decimal) or convert a decimal number to base 2 (binary). It should have four functions menu(), reverse(), base2(), and base10(). Each will be outlined in detail below. A rubric will be included at the bottom of this document. Menu() The goal of menu() is to be the function that orchestrates the flow of...
The Problem Write a C or C++ program which performs specific operations on bits. The user...
The Problem Write a C or C++ program which performs specific operations on bits. The user will specify a value The user can then set, test, or toggle individual bits in the initial value. Input Your program will prompt the user for: An inital value in hexadecimal. This value may be up to 32 bits (eight hexadecimal digits) long. If the user-supplied value is less than eight characters in length, assume the additional digits are zero and are on the...
In C++ Write a program that will convert a string of binary digits to their decimal...
In C++ Write a program that will convert a string of binary digits to their decimal equivalent. For convenience limit the binary number to 16 bits. Write the decimal equivalent to the screen. For this program you must read in the binary number as a string type. If you were to enter a large binary number like 110110001100111 as a decimal value it would be too large to fit in an int type.
C++ Fahrenheit to Celsius Tables Write a program that first asks the user which Temperature scale...
C++ Fahrenheit to Celsius Tables Write a program that first asks the user which Temperature scale conversion he/she would like to perform: 1. Convert F to C 2. Convert C to F 3. Quit What is your choice? Then it asks the user for input for three real number variables: start_temp, end_temp, temp_incr. It will then produce a two column Fahrenheit to Celsius table or a two column Celsius to Fahrenheit table, depending on the choice. For choice 1, the...
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. The suggested approach was to take the input data as a string. But I am really lost as to how to process the string and then converting it to another data type. Thanks for the help!
Batch numeric conversion: Using the code from Pgm2, create a batch conversion process which reads input...
Batch numeric conversion: Using the code from Pgm2, create a batch conversion process which reads input data (input type, input data, and output type) from a file but still outputs to the console window. For MARS, the input files are expected in the same directory (folder) where MARS resides, by default. Since the input values are not naturally displayed in the console, this program needs to reflect the input from the file as well as outputting the converted value. Upload...
Create a program called Currency Conversion. This will convert one currency to other currencies (for example...
Create a program called Currency Conversion. This will convert one currency to other currencies (for example CAD to GBP, Euro and USD).  The program should prompt user to choose currency in hand and currency required. It should also ask the user to enter amount (currency value).  The conversion rate is fixed as mentioned below. You should store the exchange rate as final values.  Use Scanner for input and printf for formatting decimals (rounded to two decimal places)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT