Question

Hello, I have a question about a C program problem below. The question is " Write...

Hello, I have a question about a C program problem below.

The question is " Write ONE C Program to accomplish the following task: Convert a double number to its hexadecimal form"

But, there are constraints below.

Constraints:
(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”.

Thanks for helping me!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

=========================

C PROGRAM

=========================

/******************************************************************************
File: DoubleToHexadecimal.c

The program converts double number >=0 and <=1000

to its hex form (upto 6 significant digits)

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

#include <stdio.h>
/*prototype for function intToHex*/
char intToHex(int);

/*main program starts*/
int main()
{
double dblValue;
long intPart;
float fractionPart;
char hex[10]; /*here the actual hex digits will be stored*/
char tempIntToHex[3]; /*temp storage for int part*/
int remain;
int i ,counter ,j;
  
printf("Enter double value: ");
scanf("%lf",&dblValue);/*read double value from user*/
  
if(dblValue >=0 && dblValue <=1000) /*check if the input number is in range [0,1000]*/
{
intPart = (int)dblValue; /*get the integer part*/
fractionPart = dblValue - intPart; /*get the fraction part*/
counter = 0; /* this will maintain total characters in hex array*/
  
/*conversion of int part to its equivalent hex form*/
while(intPart > 0)
{
remain = intPart%16;
intPart = intPart/16;
tempIntToHex[counter] = intToHex(remain);
counter++;
}
  
/*store contents of tempIntToHex into hex in reverse order*/
for(j = 0; j < counter; j++)
{
hex[j] = tempIntToHex[counter -j-1];
}
  
hex[counter]='.'; /*store the point*/
  
counter++;
  
/*now convert the fractinPart to its equivalent hex form
this will be done for max 6 significant digits only*/
for(i = 1; i <=6 ;i++)
{

fractionPart = fractionPart * 16;
/*get int part and convert it to hex digit*/
intPart = (int)fractionPart;
hex[counter] = intToHex(intPart);
fractionPart = fractionPart - intPart;/*get next fraction part*/
counter++;
/*if at any point fraction part becomes zero
before 6th iteration , break out of the loop*/
if(fractionPart == 0){
break;
}
}
  
}else{
/*the number is out of range*/
printf("Input number is outside the range [0,1000].");
return 0;
}
  
/*print the contents of hex array, which holds the hex form of input number*/
printf("\nThe hexadecimal 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 hexDigit;
  
if(num >=0 && num <=15){
if(num >=0 && num<=9){
hexDigit = num+'0';/*hexDigit will be the num itself*/
}else{
hexDigit = 'A' + (num -10); /*10 will be 'A',11 will be 'B' ,....15 will be 'F'*/
}
}
/*printf("\n%d = %c",num,hexDigit);*/
return hexDigit;
}

============================

OUTPUT

============================

RUN1

Enter double value: 456.2569

The hexadecimal number is: 1C8.41C433

RUN2

Enter double value: 999.987

The hexadecimal number is: 3E7.FCAC08

RUN3

Enter double value: 143.25

The hexadecimal number is: 8F.4

RUN4

Enter double value: 1011
Input number is outside the range [0,1000].

Add a comment
Know the answer?
Add Answer to:
Hello, I have a question about a C program problem below. The question is " Write...
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
  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of amain () function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

    Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • In this question you have to write a C++ program to convert a date from one...

    In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the string that the function...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a nain() function and a function called convert Datel). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received,...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a nain() function and a function called convert Datel). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received,...

  • Hi everyone, I have a problem about C programming. Background Material: The figure 6 is as...

    Hi everyone, I have a problem about C programming. Background Material: The figure 6 is as below: The task: Download the file 55.c. Study the C code, then build an executable and run it to see what the output is. Modify the program so that the output is The string in buffer is "In C the value of 12 + 34 / 5 is 18." Do this by looking up decimal values for ASCII codes in Figure 6 and typing...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

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