Question

Problem 2 Write a Hexadecimal to Decimal converter. Program will take an input (hexadecimal number) from user and display its c programming
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to above problem in C. Please read the code comments for more information and Please give a thumbs up if you like the solution

C CODE


#include <stdio.h>
#include<string.h>
#include<math.h>

void convert()
{
// taking input for hexadecimal number as string
printf("Enter hexadecimal number: ");
char s[1000];
scanf("%s",s);
//length of input
int len = strlen(s);
int i=len-1;
//declaring long variable to calculate the total
long total=0;
//to calculate the exponent
int c=0;
//iterating all characters in the input string
// the value of hexadecimal is calculated as
// AEFF = 10 * 16^3 + 14*16^2 + 15*16^1+ 15*16^0
while(i>=0)
{
  
if(s[i]=='F')
{
  
total+=15*pow(16,c);
}
else if(s[i]=='E')
{
total+=14*pow(16,c);
}
else if(s[i]=='D')
{
total+=13*pow(16,c);
}
else if(s[i]=='C')
{
total+=12*pow(16,c);
}
else if(s[i]=='B')
{
total+=11*pow(16,c);
}
else if(s[i]=='A')
{
total+=10*pow(16,c);
}
else if(s[i]-'0'>=0)
{
total+=(s[i]-'0')*pow(16,c);
}
i--;
c++;
}
  
printf("Decimal number to %s hexadecimal number is %ld\n", s, total);
}
int main()
{
printf("HEXADECIMAL TO DECIMAL CONVERTOR\n");
//menu driven program which runs till user exits the program
int option;
while(option!=2)
{
printf("---------MENU-----------\n");
printf("1. CONVERT A NUMBER: \n");
printf("2. EXIT\n");
printf("Enter your choice: ");
scanf("%d",&option);
switch(option)
{
  
case 1:
{
// method to convert hexadecimal to decimal
convert();
}
break;
case 2:
break;
default:
printf("Enter correct choice:\n");
}
  
}

return 0;
}


SCREENSHOT OF CODE

HEXADECIMAL TO DECIMAL CONVERTOR ---------MENU---- 1. CONVERT A NUMBER: 2. EXIT Enter your choice: 1 Enter hexadecimal number

Add a comment
Know the answer?
Add Answer to:
c programming Problem 2 Write a Hexadecimal to Decimal converter. Program will take an input (hexadecimal...
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
  • [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g` `Incorrect hex number` Hints: you...

  • [Using Python] Write a program to convert a hexadecimal number to its decimal value using the...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value using the ord builtin function (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g`...

  • Create a c++ program which: 1. Displays a menu: 1) Decimal to Binary • 2) Binary...

    Create a c++ program which: 1. Displays a menu: 1) Decimal to Binary • 2) Binary to Decimal • 3) Decimal to Hex • 4) Hex to Decimal • 9) Exit Program 2. For Menu item #1: Ask the user for a Decimal number. If they type a negative number, go back to step #1 1. Convert the Decimal number to binary and display it. 3. For Menu item #2: Ask the user for a binary number (1's and 0's)....

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • Java: can also use “if else” statements Write a program that can convert an integer between...

    Java: can also use “if else” statements Write a program that can convert an integer between 0 and 15 into hex number (including 0 and 15). The user enters an integer from the console and the program displays the corresponding hex number. If the user enters an integer out of range, the program displays a warning message about the invalid input. Table. Conversion between Decimal and Hexadecimal Decimal Hexadecimal 0 0 1 1 2 2 3 3 4 4 5...

  • C++ Functions & Streams Write a program that will demonstrate some of the C++ Library Functions,...

    C++ Functions & Streams Write a program that will demonstrate some of the C++ Library Functions, Stream Manipulators, and Selection Control Structures. The user will be given a menu of four choices. They can input a 1 for finding Cosines, 2 for finding Logarithms, 3 for converting between Decimal and Hexadecimal, or 4 to change the format of a cstring date. You must use the proper functions and/or stream manipulators to find the answers. If the user picks the cosine,...

  • Using C++ programming. Write a program that takes a string of input from the user and...

    Using C++ programming. Write a program that takes a string of input from the user and separates the string of words based on the premise that the string contains words whose first letter is uppercase. Then, display the phrase (or sentence) to a string in which the words are separated by spaces and only the first word of the phrase starts with an uppercase letter. For example, if the user enters "IAmTheTeacher", then the program would display: "I am the...

  • Write an interactive C++ program that asks a user to input the color code of a...

    Write an interactive C++ program that asks a user to input the color code of a resistor and determines its value and tolerance Tell the user how to input the color rings “Instructions” Tell the user to input the colors one after the other After the program receives the information it will display the resistance and tolerance Output an error message if the user enters an invalid color code (no garbage values should be displayed), and ask for another input...

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