Question

For integer n € {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) function hex(n) returns hexadecimal character from F. Write function
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWERS:

As per your requirement here is the c++ code to convert an integer to hex and terminating the code if the user enters 0

Raw code:

#include<iostream> //input output stream

using namespace std; //name space standard

void Hex(int n) //hex function with one parameter

{

  char hexa_Decima_Number[100]; //char array to store the resultant hexa hexa_Decima_Number

  int i = 0; //count variable

  while(n!=0)//while loop

  {

    int temp_value = 0; //temp_value as 0

    

    temp_value = n % 16; // store remainder in temp_value variable.

    if(temp_value < 10) // check if temp_value < 10

    {

      hexa_Decima_Number[i] = temp_value + 48; //converstion of values using ascii

      i++; //incrementing i value

    }

    else//else condition

    {

      hexa_Decima_Number[i] = temp_value + 55; //to convert character values using ascii

      i++; //incrementing

    }

    n = n/16; //because its hex means 16

  }

  cout<<"Hexa Decimal value= ";

  for(int k=i-1; k>=0; k--) //to get hexa_Decima_Number in reverse order

    cout <<hexa_Decima_Number[k]; //printing

}

int main() //main

{

  int n = 1; //initialize n as 1

while(n){//while it runs infinitely because it is a true always

cout<<"Enter a positive integer: ";//prompt the user to enter integer

cin>>n;//taking input from user

if(n==0){ //if user enters 0 break the loop

cout<<"\nDone!";

break;//break

}

Hex(n);//calling the function by sending user input as parameter

cout<<endl; //to print on next line

}

  return 0;

}

code in the editor:

main.cpp saved i #include<iostream> //input output stream 2 using namespace std; //name space standard 3 void Hex(int n) //he

output:

clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE_700/final) ? clang++-7 -pthread -o main main.cpp > ./main Enter a positiv

Feel free to comment in the comment section if you have any doubts or queries.

Thank you! doupvote.

Add a comment
Know the answer?
Add Answer to:
For integer n € {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) function hex(n) returns hexadecimal character from F. Write function hex and...
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
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