Question

In: Computer Science

Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...

Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form

1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345

2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45".

3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45".

Please write as simple as possible because I am a freshman in C!

Thanks!

Solutions

Expert Solution

A C program is given below that will take a number of type double between 0 and 1000 and converts the number in its equivalent hexadecimal form with six significant digits. The integer part and fraction part in the double number are converted separately. The whole procedure is shown in the program along with its description in the comments.

Program code:

/******************************************************************************

The program converts double number between 0 to 1000 to its hexadecimal form (upto 6 significant digits)

*******************************************************************************/

#include <stdio.h>

/*prototype for function intToHex*/

char intToHex(int);

/*main program starts*/

int main()

{

    /*variables are declared*/

double d_value;

long i_part;

float f_part;

char hex[10]; /*the actual hex digits are stored in this array*/

char tempFHex[3]; /*temp storage for int part*/

int rem;

int i , j, counter;

printf("Enter double number (x): ");

scanf("%lf",&d_value);/*enter value from user*/

if(d_value >=0 && d_value <=1000) /*check whether the input number is in the given range [0,1000]*/

{

i_part = (int)d_value; /*getting the integer part*/

f_part = d_value - i_part; /*getting the fraction part*/

counter = 0; /* to maintain total characters in hex array*/

/* int part is converted into its equivalent hex form*/

while(i_part > 0)

{

rem = i_part%16;

i_part = i_part/16;

tempFHex[counter] = intToHex(rem);

counter++;

}

/*contents of tempFHex are stored into hex in reverse order*/

for(j = 0; j < counter; j++)

{

hex[j] = tempFHex[counter -j-1];

}

hex[counter]='.'; /*store the point*/

counter++;

/*The fraction Part is converted into its equivalent hex form and

this will be done for max 6 significant digits only*/

for(i = 1; i <=6 ;i++)

{

f_part = f_part * 16;

/*get int part and convert it to hex digit*/

i_part = (int)f_part;

hex[counter] = intToHex(i_part);

f_part = f_part - i_part;/*get next fraction part*/

counter++;

/*if at any point fraction part becomes zero

before 6th iteration , break out of the loop*/

if(f_part == 0){

break;

}

}

}

else{

/*the number is out of range*/

printf("The given number is out of the range [0,1000].");

return 0;

}

/*print the contents of hex array, which holds the hex form of input number*/

printf("\nThe hexadecimal number for the given double number is: ");

for(i = 0; i < counter ; i++)

{

printf("%c",hex[i]);

}

return 0;

}

/*function to convert num in 0 to 15 to its equivalent hex digit*/

char intToHex(int num)

{

char h_digit;

if(num >=0 && num <=15){

if(num >=0 && num<=9){

h_digit = num+'0';/*h_digit will be the num itself*/

}

else{

h_digit = 'A' + (num -10); /*10 will be 'A',11 will be 'B' ,....15 will be 'F'*/

}

}

/*printing final hexadecimal number*/

return h_digit;

}

Sample Outputs:

Output1: When number is within the range [0 to 1000]

Output2: When number is out of range [0 to 1000].


Related Solutions

Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form 1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345 2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45". 3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45". Please write as simple as possible because I am a freshman in C! And please give me some clues! Thanks!
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
Number conversion between hexadecimal and binary. Show the working steps. a) Convert the hexadecimal number E4D2...
Number conversion between hexadecimal and binary. Show the working steps. a) Convert the hexadecimal number E4D2 into binary. b) Convert the binary number 1100010111110011 into hexadecimal
Convert the hexadecimal number directly to base 4; then convert both the original number and your...
Convert the hexadecimal number directly to base 4; then convert both the original number and your answer to binary to check your result. Please show steps and explain
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature to Fahrenheit temperature. The formula for converting Celsius temperature into Fahrenheit temperature is:    F = (9 / 5) * C + 32 Create integer constants for the 3 numbers in the formula (9, 5, and 32).  Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. Convert the 9 to a double when converting the temperature (use type casting). Have a...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal. Your main function should a) declare a char array b) call the readLn function to read from the keyboard, c) call a function to convert the input text string to an int, d) multiply the int by ten, e) call a function to convert the int to its corresponding hexadecimal text string, f)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT