Question

need help with writing a program that converts i.e. binary to decimal or decimal to hexadecimal...

need help with writing a program that converts i.e. binary to decimal or decimal to hexadecimal but cannot use the conversion tool built-in Java and please use the switch and write comments to understand.
1 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       //for taking console input
       Scanner sc = new Scanner(System.in);
       int choice, value;
      
       while(true) {//infinite loop
           System.out.println("1. Binary to Decimal");
           System.out.println("2. Decimal to Hexadecimal");
           System.out.println("3. Exit");
           System.out.print("> ");
           choice = sc.nextInt(); sc.nextLine();//taking user choice
          
           switch(choice) {
           case 1://converting binary to decimal
               System.out.print("Enter Binary Number: ");
               value = sc.nextInt(); sc.nextLine();
               System.out.println("Decimal: "+binToDec(value));
               System.out.println();
               break;
              
           case 2://converting decimal to hexadecimal
               System.out.print("Enter Decimal Number: ");
               value = sc.nextInt(); sc.nextLine();
               System.out.println("Hexadecimal: "+decToHex(value));
               System.out.println();
               break;
              
           case 3://exit condition
               System.out.println("Good Bye");
               System.exit(0);
              
           default://choice doesn't match with cases
               System.out.println("Invalid Input!");
               System.out.println();
               break;
           }
       }
   }

   public static int binToDec(int binary) {
       int decimal = 0;
       int power = 0;
       while (true) {//infinte loop
           if (binary == 0) {
               break;//get out of loop when binary becomes zero
           } else {
               int temp = binary % 10;//extrating single digit from binary number
               decimal += temp * Math.pow(2, power);//adding the decimal form
               binary = binary / 10;
               power++;//increasing the power
           }
       }
       return decimal;
   }

   public static String decToHex(int decimal) {
       int remainder;
       String hex = "";//for holding hexadecimal
       //to select the value by providing the index
       char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
      
       while (decimal > 0) {//loop that will run till decimal is greater than zero
          
           remainder = decimal % 16;
           //selecting the value from hexChar and adding it to hex
           hex = hexChars[remainder] + hex;
           decimal = decimal / 16;
       }
       return hex;
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
need help with writing a program that converts i.e. binary to decimal or decimal to hexadecimal...
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
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