answersLogoWhite

0

import java.util.Scanner;

public class NumberSystem

{

public void displayConversion()

{

Scanner input = new Scanner(System.in);

System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal",

"Binary", "Octal", "Hexadecimal");

for ( int i = 1; i <= 256; i++ )

{

String binary = Integer.toBinaryString(i);

String octal = Integer.toOctalString(i);

String hexadecimal = Integer.toHexString(i);

System.out.format("%-20d%-20s%-20s%-20s\n", i,

binary, octal, hexadecimal);

}

}

// returns a string representation of the decimal number in binary

public String toBinaryString( int dec )

{

String binary = " ";

while (dec >= 1 )

{

int value = dec % 2;

binary = value + binary;

dec /= 2;

}

return binary;

}

//returns a string representation of the number in octal

public String toOctalString( int dec )

{

String octal = " ";

while ( dec >= 1 )

{

int value = dec % 8;

octal = value + octal;

dec /= 8;

}

return octal;

}

public String toHexString( int dec )

{

String hexadecimal = " ";

while ( dec >= 1 )

{

int value = dec % 16;

switch (value)

{

case 10:

hexadecimal = "A" + hexadecimal;

break;

case 11:

hexadecimal = "B" + hexadecimal;

break;

case 12:

hexadecimal = "C" + hexadecimal;

break;

case 13:

hexadecimal = "D" + hexadecimal;

break;

case 14:

hexadecimal = "E" + hexadecimal;

break;

case 15:

hexadecimal = "F" + hexadecimal;

break;

default:

hexadecimal = value + hexadecimal;

break;

}

dec /= 16;

}

return hexadecimal;

}

public static void main( String args[])

{

NumberSystem apps = new NumberSystem();

apps.displayConversion();

}

}

User Avatar

Darien Renner

Lvl 10
3y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
RossRoss
Every question is just a happy little opportunity.
Chat with Ross

Add your answer:

Earn +20 pts
Q: Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Other Math