Hexadecimal and binary Learn programming Java

Lesson:

Basic Data Types


Exercise:

Hexadecimal and binary 39


Objetive:

Create a java program to ask the user for a number an display it both in hexadecimal and binary. It must repeat until the user enters 0.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int n;
		do
		{
			System.out.print("Enter a number:");
			n = Integer.parseInt(new Scanner(System.in).nextLine());

			if (n != 0)
			{
				System.out.print("Hexadecimal: ");
				System.out.println(String.valueOf(n, 16));
				System.out.print("Binary: ");
				System.out.println(String.valueOf(n, 2));
			}
		} while (n != 0);
	}
}