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

Write a program that allows numbers to be entered in decimal (base 10), hexadeci

ID: 3568339 • Letter: W

Question

Write a program that allows numbers to be entered in decimal (base 10), hexadecimal (base 16), or binary (base 2), and then prints out the number again in all three bases.

To distinguish between different bases we'll say that hexadecimal numbers should always be preceded by a "$" and binary numbers by a "%". Other numbers are assumed to be decimal.

When the program is run it should repeatedly allow a number to be entered and then print it out in all three bases until "q" is typed in. Here's an example of how your program should operate:

Enter #: 45

    Decimal: 45

    Hexadecimal: 2D

    Binary: 101101

Enter #: %101

    Decimal: 5

    Hexadecimal: 5

    Binary: 101

Enter #: $31

    Decimal: 49

    Hexadecimal: 31

    Binary: 110001

Enter #: q

Explanation / Answer

////////////////////////////////////////    Code in java    //////////////////////////////////////////////////

////////////////////////////////////////    Converter.java    //////////////////////////////////////////////////

import java.util.Scanner;

public class Converter {
   public static void main(String args[]) {
      
       // Scanner to to get user input
       Scanner s = new Scanner(System.in);

       do {
           System.out.print("Enter #: ");
          
           // Storing user input in inp
           String inp = s.nextLine();

           CharSequence hex = "$";
           CharSequence bin = "%";

           // Checking if input contains character '$'
           if (inp.contains(hex)) {
              
               // Remove '$' from the string and store it in rep
               String rep = inp.replace("$", "");
              
               // converting rep (a hex number) into integer
               int i = Integer.parseInt(rep, 16);

               // Printing integer i
               System.out.println(" Decimal: " + i);
               // Printing after converting into hex
               System.out.println(" Hexadecimal: " + Integer.toHexString(i));
               // Printing after converting into bin
               System.out.println(" Binary: " + Integer.toBinaryString(i));
           } else if (inp.contains(bin)) {
               String rep = inp.replace("%", "");
               int i = Integer.parseInt(rep, 2);

               System.out.println(" Decimal: " + i);
               System.out.println(" Hexadecimal: " + Integer.toHexString(i));
               System.out.println(" Binary: " + Integer.toBinaryString(i));
           } else {
               // Breaking the ever ending do-while loop if user input is "q"
               if (inp.equalsIgnoreCase("q"))
                   break;
               int i = Integer.parseInt(inp);

               System.out.println(" Decimal: " + i);
               System.out.println(" Hexadecimal: " + Integer.toHexString(i));
               System.out.println(" Binary: " + Integer.toBinaryString(i));
           }
       } while (true);
       s.close();
   }

}

////////////////////////////////////////    Code in MATLAB   //////////////////////////////////////////////////

////////////////////////////////////////    converter.m   //////////////////////////////////////////////////

function converter()
do
   condition = 3;
  
   userInput = input("Enter #: ", "s");
   if(index(userInput, 'q') > 0)
       % quit case
       condition = 0;
   end
   if(index(userInput, '$') > 0)
       % hex case
       condition = 1;
   end
   if(index(userInput, '%') > 0)
       % bin case
       condition = 2;
   end
  
   switch (condition)
       case 1
           temp = strrep(userInput, '$', '');
           showResult(hex2dec(temp));
       case 2
           temp = strrep(userInput, '%', '');
           showResult(bin2dec(temp));
       case 3
           showResult(str2num(userInput));
       otherwise
           % Nothing to do
   endswitch
until(condition==0);
endfunction

function showResult(number)
   fprintf(" Decimal: %d ", number);
   fprintf(" Hexadecimal: %s ", dec2hex(number));
   fprintf(" Binary: %s ", dec2bin(number));
endfunction

////////////////////////////////////////    Sample    //////////////////////////////////////////////////

Enter #: 45
   Decimal: 45
   Hexadecimal: 2d
   Binary: 101101
Enter #: %101
   Decimal: 5
   Hexadecimal: 5
   Binary: 101
Enter #: $31
   Decimal: 49
   Hexadecimal: 31
   Binary: 110001
Enter #: 31
   Decimal: 31
   Hexadecimal: 1f
   Binary: 11111
Enter #: 101
   Decimal: 101
   Hexadecimal: 65
   Binary: 1100101
Enter #: $45
   Decimal: 69
   Hexadecimal: 45
   Binary: 1000101
Enter #: q

////////////////////////////////////////   ----------    //////////////////////////////////////////////////

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote