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...

  • 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...

  • Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs...

    Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs six numbers [Keep the numbers in an array]. The numbers must be between 20 and 200, inclusive. If the number is not between 20 and 200, ask for another input. When a number is entered, display the number only if it is not a duplicate of a number already entered. If it is a duplicate of a number already entered, display a message that...

  • Write a MARIE program to implement one round of rock paper scissors game. Your program should...

    Write a MARIE program to implement one round of rock paper scissors game. Your program should represent the three moves ‘rock’, ‘paper’ and ‘scissors’ with numbers 1, 2 and 3 respectively. When the program is run, there should be two input prompts, one after the other, to ask for player 1’s and player 2’s moves (a number 1, 2 or 3). Then the program would compare both numbers and apply the following rules to work out the winner. Paper beats...

  • Problem: You will write a program a C++ to compute some statistics based on monthly average...

    Problem: You will write a program a C++ to compute some statistics based on monthly average temperatures for a given month in each of the years 1901 to 2016. The data for the average August temperatures in the US has been downloaded from the Climate Change Knowledge Portal, and placed in a file named “tempAugData.txt”, available on the class website. The file contains a sequence of 116 values. The temperatures are in order, so that the first one is for...

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