Decimal to hexadecimal conversion program in java. For example F is hexadecimal representation of decimal number 15.
import java.util.Scanner; public class Conversion { public static void main(String[] args) { int Number,Remainder; Scanner input=new Scanner(System.in); System.out.print("enter Decimal Number:"); Number=input.nextInt(); input.close(); String Hex=""; while(Number!=0) { Remainder=Number%16; switch(Remainder) { case 10: Hex="A"+Hex; break; case 11: Hex="B"+Hex; break; case 12: Hex="C"+Hex; break; case 13: Hex="d"+Hex; break; case 14: Hex="E"+Hex; break; case 15: Hex="F"+Hex; break; default: Hex=Remainder+Hex; break; } Number=Number/16; } System.out.println("Hexadecimal representation is "+Hex); } }
enter Decimal Number:15 Hexadecimal representation is F ------------------------------- enter Decimal Number:255 Hexadecimal representation is FF -------------------------------- enter Decimal Number:127 Hexadecimal representation is 7F