Question

In: Computer Science

Write a program that receives an integer and must return a string containing the hexadecimal representation...

Write a program that receives an integer and must return a string containing the hexadecimal representation of that integer.

Solutions

Expert Solution

If you have any doubts, please give me comment...

//In this assignment, we write code to convert decimal integers into hexadecimal numbers

#include <iostream>

#include <string>

using namespace std;

//convert the decimal integer d to hexadecimal, the result is stored in hex

string dec_hex(int d)

{

    char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    int k = 0;

    string hex = "";

    while (d > 0)

    {

        int rem = d % 16;

        hex = digits[rem]+hex;

        d = d / 16;

        k++;

    }

    return hex;

}

int main()

{

    int d;

    string hex;

    cout<<"Enter an integer : ";

    cin>>d;

    hex = dec_hex(d);

    cout<<"Decimal: "<<d<<", Hexadecimal: "<<hex<<endl;

    return 0;

}


Related Solutions

Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Checker for integer string Forms often allow a user to enter an integer. Write a program...
Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
# Write three functions to return the pieces of a string containing # a city, state,...
# Write three functions to return the pieces of a string containing # a city, state, and zip code. # This function needs a better comment def get_zip(city_state_zip):    return city_state_zip[-1] # This function needs a better comment def get_city(city_state_zip): return city_state_zip[0:2] # This function needs a better comment def get_state(city_state_zip):    return 'Confusion' def main():    place1 = 'Rolla, MO 65402'    place2 = 'Cape Girardeau,     MO 63780'    place3 = 'St. Louis,MO63111'    place4 = 'International Falls,MN               56650     '   # Print the individual components   ...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT