Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop an algorithm to convert a decimal number between 0 and 255 to a hexadeci

ID: 670080 • Letter: D

Question

Develop an algorithm to convert a decimal number between 0 and 255 to a hexadecimal number using the if-else statement. Notice that you are required to use the if-else statement for this lab. You are not allowed to use Java library methods such printf() or any other utilities to do the conversion. The Decimal to Hexadecimal table is provided for your convenience. After you have derived the algorithm, write a Java program to implement your algorithm. The program asks the user to enter a decimal number between 0 and 255. The program takes the user input, runs it through your algorithm, and then prints out the result in hex value. Your code must produce the output with the format as shown in the following sample test cases. Enter a decimal between 0 and 255: 3 Decimal 3 = Hexadecimal 3 Enter a decimal between 0 and 255: 13 Decimal 13 = Hexadecimal D Enter a decimal between 0 and 255: 16 Decimal 16 = Hexadecimal 10 Enter a decimal between 0 and 255: 255 Decimal 255 = Hexadecimal FF

Explanation / Answer

Algorithm to convert decimal value to hexa value

Algorithm:
Step1: Read an integer value in between 0 -255
Step2: Copy input into another variable to process
        Create a character array to store the hexa decimal value
Step3: Run while with conition that the vlaue is not zero
Step3.1:Get remainder by apply modulo operator %
Step3.2 : call the method getHexaCharacter that takes remainder
and returns the equivalent to decimal value in hexa number system.
Step3.3 : Add hexa character to the string value.
Step3.4 :Get next quotient value by dividing by 16
Step3.5 : Repeat the while loop until the quotient is not zero.
Step4: Print the decimal equivalent hexa number.

----------------------------------------------------------------------------------------------------------------------------------------------------
/**The java program Decimal2Hexa prompts user to enter a decimal value
* and prints its equivalent hexa value .*/
//Decimal2Hexa.java
import java.util.Scanner;
public class Decimal2Hexa
{
   public static void main(String[] args)
   {
       //Create a Scanner class obejct
       Scanner scanner =new Scanner(System.in);
       //character array to store hexa value
       char[] hexa=new char[100];
       //declare integer variables
       int userInput;      
       int quotient;
       int size=0;
      
      

       //prompt user until input is 0 -255
       do
       {
           System.out.print("Enter a decimal between 0 and 255 :");
           userInput=scanner.nextInt();

       }while(userInput<0 || userInput>255);

       quotient = userInput;
       while(quotient!=0)
       {
           //Get remainder by applying modulo operator
           int rem = quotient % 16;

           //calling getHexaCharacter of remainder value
           char equivalent=getHexaCharacter(rem);
           //set value
           hexa[size]+= equivalent;
           size++;
           //get next quotient
           quotient = quotient / 16;
       }

       //print deciaml equivalent of hexa decimal value
       System.out.print("Decimal "+userInput+" = Hexadecimal ");
      
       //print the hexa number
       for (int i = size-1; i >=0; i--)       
           System.out.print(hexa[i]);
      
   }

   /*The method takes integer value and returns the equivalent
   * hexa deciaml value in return*/
   public static char getHexaCharacter(int decimal)
   {
       char equivalent=' ';
       switch(decimal)
       {
       case 0:
           equivalent= '0';
           break;
       case 1:
           equivalent= '1';
           break;
       case 2:
           equivalent= '2';
           break;
       case 3:
           equivalent= '3';
           break;
       case 4:
           equivalent= '4';
           break;
       case 5:
           equivalent= '5';
           break;
       case 6:
           equivalent= '6';
           break;
       case 7:
           equivalent= '7';
           break;
       case 8:
           equivalent= '8';
           break;
       case 9:
           equivalent= '9';
           break;
       case 10:
           equivalent= 'A';
           break;
       case 11:
           equivalent= 'B';
           break;
       case 12:
           equivalent= 'C';
           break;
       case 13:
           equivalent= 'D';
           break;
       case 14:
           equivalent= 'E';
           break;
       case 15:
           equivalent= 'F';
           break;
       }
       //returns the equivalent character of decimal in hexa
       return equivalent;
   }//end of the getHexaCharacter
  
}//end of the class


----------------------------------------------------------------------------------------------------------------

Sample run1:
Enter a decimal between 0 and 255 :3
Decimal 3 = Hexadecimal 3

Sample run2:
Enter a decimal between 0 and 255 :13
Decimal 13 = Hexadecimal D

Sample run3:
Enter a decimal between 0 and 255 :16
Decimal 16 = Hexadecimal 10

Sample run4:
Enter a decimal between 0 and 255 :255
Decimal 16 = Hexadecimal FF

Hope this helps you