Question

In Java write a program for a “calculator” that performs any conversion between decimal / binary...

In Java write a program for a “calculator” that performs any conversion between decimal / binary / hexadecimal values that are provided as inputs by the user.

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

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import java.util.Scanner;
class Main
{
   public static void main (String[] args)
   {
   Scanner sc= new Scanner(System.in);
  
   //Menu
   System.out.println("1. Decimal To Binary ");
   System.out.println("2. Decimal To Hexadecimal ");
   System.out.println("3. Binary To Decimal ");
   System.out.println("4. Binary To Hexadecimal ");
   System.out.println("5. Hexadecimal To Decimal ");
   System.out.println("6. Hexadecimal To Binary ");
   System.out.println("7. Exit ");
  
   //LOOP through the user choice
   while(true)
   {
   System.out.print("Choose the option: ");
   int choice = sc.nextInt();
   if(choice==7)
   break;
   System.out.print("Enter Number: ");
   String num;
   int n;
   switch(choice)
   {
   case 1: num=sc.next();
   n=Integer.parseInt(num,10);
   System.out.println("The Binary Form is : "+Integer.toBinaryString(n));
   break;
   case 2: num=sc.next();
   n=Integer.parseInt(num,10);
   System.out.println("The Hexadecimal Form is : "+Integer.toHexString(n));
   break;
  
   case 3: num=sc.next();
   n=Integer.parseInt(num,2);
   System.out.println("The Decimal Form is : "+n);
   break;
  
   case 4: num=sc.next();
   n=Integer.parseInt(num,2);
   System.out.println("The Hexadecimal Form is : "+Integer.toHexString(n));
   break;
  
   case 5: num=sc.next();
   n=Integer.parseInt(num,16);
   System.out.println("The Decimal Form is : "+n);
   break;
  
   case 6: num=sc.next();
   n=Integer.parseInt(num,16);
   System.out.println("The Binary Form is : "+Integer.toBinaryString(n));
   break;
   default: break;
   }
   }
   }
}

====================

SCREENSHOT:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In Java write a program for a “calculator” that performs any conversion between decimal / binary...
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