Problem

In Listing 1, how many times the loop body is executed for a decimal number 245 and how ma...

In Listing 1, how many times the loop body is executed for a decimal number 245 and how many times the loop body is executed for a decimal number 3245?

LISTING 1 Dec2Hex.java

1 import java.util.Scanner;

2

3 public class Dec2Hex {

4      /** Main method */

5      public static void main(String[] args) {

6          // Create a Scanner

7          Scanner input = new Scanner(System.in);

8

9          // Prompt the user to enter a decimal integer

10        System.out.print("Enter a decimal number: ");

11        int decimal = input.nextInt();

12

13        // Convert decimal to hex

14        String hex = "";

15

16        while (decimal != 0) {

17            int hexValue = decimal % 16;

18

19            // Convert a decimal value to a hex digit

20            char hexDigit = (hexValue <= 9 && hexValue >= 0) ?

21               (char)(hexValue + '0') : (char)(hexValue - 10 + 'A');

22

23            hex = hexDigit + hex;

24               decimal = decimal / 16;

25            }

26

27            System.out.println("The hex number is " + hex);

28    }

29 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
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