Use for loop, while loop, and do while loops to generate a table of decimal numb
ID: 3798938 • Letter: U
Question
Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers, in the range 1-256. The Main program should prompt the user for input.
A sample output is shown below.
Note: To generate the binary numbers your code should use a while loop to generate the binary numbers, a “for” loop to generate the octal numbers and a “do .. while” to generate the hexadecimal numbers.
Sample Output:
Enter the low number: 1
Enter the high number: 10
Decimal Binary Octal Hexadecimal
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
****Resetting low and high ****
Low = 10, High = 15
Decimal Binary Octal hexadecimal
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
Explanation / Answer
Java Program for printing the decimal, Binary, Octal , Hexa-decimal representation of range of numbers:-
import java.util.Scanner;
public class BinayOctalHex {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
//Scan the low and high values
System.out.println("Enter the From Value:- " );
Integer low = sc.nextInt();
System.out.println("Enter the To Value:- " );
Integer high = sc.nextInt();
//Using For loop to loop from Low to high values
for(Integer i = low ; i<= high ;i++){
//using the Integer CLass Functions for getting the required Outputs.
int decimal = i;
String binary = i.toBinaryString(i).toUpperCase();
String Octal = i.toOctalString(i).toUpperCase();
String Hex = i.toHexString(i).toUpperCase();
//Printing the results to the Console
System.out.println(decimal+" "+binary+" "+Octal+" "+Hex);
}
}
}
Output:-
Enter the From Value:-
1
Enter the To Value:-
10
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
Enter the From Value:-
10
Enter the To Value:-
15
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.