Write a c++ program to display a value in decimal, octal, and hexadecimal. The p
ID: 653637 • Letter: W
Question
Write a c++ program to display a value in decimal, octal, and hexadecimal.
The program must:
1. not use cout more than twice:
2. declare one type in variable
3. use count to prompt the user to enter any deimal integer numeric value:
4. use cin to obtain and store the user input value in the type int variable;
5. use cout to deplay the input value in decimal, octal, and hexadecimal using hte follogin fourmat where D represents the decimal value, 0 represents the octal value, and H represents the hexadecimal value. D decimal = 0 octal = H hexadecimal
for example, if the user where to enter 22 the program would display the following, all on one line:
22 decimal = 26 octal = 16 hexadecimal
Explanation / Answer
#include<iostream.h>
#include<conio.h>
int convertToOctal(int n) /* Function to convert decimal to octal */
{
int rem; //declare varaiable to store remainder
int i=1; //declare and initialize varaiable i to 1
int octal=0; //declare varaiable to store the octal representation of decimal input number
while (n!=0) //keep on looping and dividing until the input decimal number becomes 0
{
rem=n%8; //divide the number by 8 and store its remainder in variable rem
n/=8; //divide the number by 8 and store its quotient in n
octal+=rem*i; //evaluate octal=octal+rem*i
i*=10; //i=i*10;
}
return octal; //return the octal representation of the number
}
int convertToHexaDecimal(int n) /* Function to convert decimal to octal */
{
int rem; //declare varaiable to store remainder
int i=1; //declare and initialize varaiable i to 1
int hexadecimal=0; //declare varaiable to store the hexadecimal representation of decimal input number
while (n!=0) //keep on looping and dividing until the input decimal number becomes 0
{
rem=n%16; //divide the number by 16 and store its remainder in variable rem
n/=16; //divide the number by 16 and store its quotient in n
hexadecimal+=rem*i; //evaluate hexadecimal=hexadecimal+rem*i
i*=10; //i=i*10;
}
return hexadecimal; //return the hexadecimal representation of the number
}
void main(){
int var; //declare varaiable to store input decimal number
clrscr(); //clear the screen
cout<<"Enter any decimal integer numeric value: "; //prompt user to enter decimal number
cin>>var; //accept the input and store in var
cout<<"D "<<var<<" O "<<convertToOctal(var)<<" H "<<convertToHexaDecimal(var); //display the decimal number and its octal and hexadeciaml representaion
getch(); //wait for any key to be pressed to exit from the console
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.