Question

In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

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

import java.util.Scanner;

class Main {
public static void convertToHexa(int n)
{
// If number is greater than 15 then calling it recursively with n/16
if(n>15)
convertToHexa(n/16);
  
// printing Hexadecimal numbers which is reminder
if(n%16 == 10)
System.out.print("A");
else if(n%16 == 11)
System.out.print("B");
else if(n%16 == 12)
System.out.print("C");
else if(n%16 == 13)
System.out.print("D");
else if(n%16 == 14)
System.out.print("E");
else if(n%16 == 15)
System.out.print("F");
else
System.out.print(n%16+"");
}

public static void main(String[] args) {
  
// declaring scanner
Scanner sc = new Scanner(System.in);

// taking user input
System.out.print("Enter an integer: ");
int num = sc.nextInt();

// printing output
System.out.print("Hexadecimal: ");
convertToHexa(num);
}
}

/*SAMPLE OUTPUT
Enter an integer: 23451
Hexadecimal: 5B9B
*/

Add a comment
Know the answer?
Add Answer to:
In Java, write a recursive method that converts an integer to hexadecimal. The function should print...
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
  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • A) Write a function that receives an integer decimal number and converts it to its Hexadecimal...

    A) Write a function that receives an integer decimal number and converts it to its Hexadecimal equivalent B) Write a function that converts a binary number to its corresponding Decimal equivalent BOTH A AND B IN ARDUINO C LANGUAGE

  • [10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For...

    [10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For example, reverse("UBC-O") displays O-CBU. Use a helper method to improve the performance of your program. Write a test program that prompts the user to enter a string and displays its reversal. MUST HAVE A HELPER METHOD TO IMPROVE PERFORMANCE. FOR JAVA THANK YOU!

  • Please help me with this question Write a recursive method that returns the largest integer in...

    Please help me with this question Write a recursive method that returns the largest integer in an array. Write a test program that prompts the user to enter a list of eight integers and displays the largest largest element. Thank you! and can you give details so I know what is going on?

  • NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

    NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

    *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...

  • 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...

    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 use it to create the function HEX(N) that for integer N>0 displays its value hexadecimal form. For all programs use C++, and make all programs as short as possible. Write the main program that supports the following sample dialog: Enter a positive integer : 13 Hexadecimal value D Enter a positive integer : 255 Hexadecimal value Enter a positive integer ; 1234567 Hexadecimal value...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

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