Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that prompts the user to enter a hexadecimal value, multipli

ID: 669364 • Letter: W

Question

Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal.

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

int hex_to_dec(char ch[]);

int main()
{
   char hex[10] = { '' };

   cout << "Enter a hex number: ";
   cin >> hex;

   int int_value = hex_to_dec(hex);

   cout << "The hex number is: " << hex << endl;

   system("pause");
   return 0;
}

int hex_to_dec(char ch[])
{
   int len = strlen(ch);
   int sum = 0;

   for (int i = 0; i < len; i++)
   {
       char hex_digit = ch[len - i - 1];

      
       int dec = 10 * i;

       sum = sum + dec;

       cout << hex_digit << endl;
  
   }

   return sum;
}

Explanation / Answer

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

unsigned long convtodecnum(char hex[]);

int main()
{

long decnum;
char hex[9];
  
cout<<" Enter 32-bit Hexadecimal Number : ";
cin>>hex;
long int remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];

decnum = convtodecnum(hex);
cout<<"Value in Decimal Number is "<<decnum<<" ";
decnum=decnum*10;
quotient = decnum;

while(quotient!=0)
{
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
  
  
cout<<"nEquivalent hexadecimal number of "<<decnum<<" is : ";
for(j = i -1 ;j> 0;j--)
cout<<hexadecimalNumber[j];
cout<<" ";
   return 0;

}

unsigned long convtodecnum(char hex[])
{
char *hexstr;
int length = 0;
const int base = 16;
long decnum = 0;
int i;
  
  
for (hexstr = hex; *hexstr != ''; hexstr++)
{
   length++;
}
  
  
hexstr = hex;
for (i = 0; *hexstr != '' && i < length; i++, hexstr++)
{
  
   if (*hexstr >= 48 && *hexstr <= 57)   
   {
   decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
   }
   else if ((*hexstr >= 65 && *hexstr <= 70))   
   {
   decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
   }
   else if (*hexstr >= 97 && *hexstr <= 102)
   {
   decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
   }
   else
   {
   cout<<"Invalid Hexadecimal Number ";

   }
}
return decnum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote