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! And please give me some clues!

Thanks!

Solutions

Expert Solution

Code:

#include <stdio.h>

#include <string.h>

int main()

{

double x;

printf("Enter the value of x: ");

scanf("%lf", &x);

char hex[30];

snprintf(hex, sizeof(hex), "%A", x); //saving hex equivalebt to hex

char hexFormat[30]; //creating new char array hexFormat to print hex in proper Format

int i, index1 = 0, index2 = 0;

//to find position of '.' or end of hex value

for(i=0; i < strlen(hex); i++)

{

    if(hex[i] == '.')

      index1 = i;

    if(hex[i] == 'P')

      index2 = i;

}

//saving to hexFormat

if(index1 != 0)

    for(i=2; i < index1+7; i++)

       hexFormat[i-2] = hex[i];

else

     for(i=2; i < index2; i++)

      hexFormat[i-2] = hex[i];

hexFormat[i-2] = '\0';

printf("Hex equivalent of x = %s\n\n", hexFormat);

}

Output:



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