Question

By using C++, write a program and show the program output that convert any hexadecimal expansion...

By using C++, write a program and show the program output that convert any hexadecimal expansion to:

i) binary expansion,

ii) octal expansion, and

iii) decimal expansion.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#include<iostream>
#include<string.h>
using namespace std;
void decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
  
// counter for binary array
int i = 0;
while (n > 0) {
  
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
cout<<"Binary is ";
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
cout<<endl;
}
void decToOctal(int n)
{
  
// array to store octal number
int octalNum[100];
  
// counter for octal number array
int i = 0;
while (n != 0) {
  
// storing remainder in octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
cout<<"Octal is ";
// printing octal number array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << octalNum[j];
cout<<endl;
}
// Function to convert hexadecimal to decimal
int hexadecimalToDecimal(char hexVal[])
{
int len = strlen(hexVal);
  
// Initializing base value to 1, i.e 16^0
int base = 1;
  
int dec_val = 0;
  
// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;
  
// incrementing base by power
base = base * 16;
}
  
// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;
  
// incrementing base by power
base = base*16;
}
}
  
return dec_val;
}
  
// Driver program to test above function
int main()
{
char hexNum[] = "1A";   
int dec=hexadecimalToDecimal(hexNum);
decToBinary(dec);
decToOctal(dec);
cout << "Decimal is "<<dec<< endl;
  
  
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
By using C++, write a program and show the program output that convert any hexadecimal expansion...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT