Q. Write a program in Java to input a number in decimal number system and convert it into
its equivalent number in the Hexadecimal number system.Hexadecimal Number system is a number
system which can represent a number in any other number system in terms of digits ranging from
0 to 9 and then A-F only.This number system consists of only sixteen basic digits i.e. 0,1,
2,3,4,5,6,7,8,9,A,B,C,D,E and F.Here 10 is represented as A,11 as B and so on till 15 which is
represented as F.For example :47 in hexadecimal number system can be represented as 2F in the
Hexadecimal number system.
import java.io.*;
public class DecimalToHexa
{
public static void main(String args[])throws Exception
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
System.out.print("Enter the number in decimal number system:");
int n=Integer.parseInt(stdin.readLine());
int r;
String s=""; //variable to store the result after
//sorting the digits as in hexadecimal number system
char dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(n>0)
{
r=n%16; //finding the remainder
s=dig[r]+s; //adding the remainder to the result
n=n/16;
}
System.out.print("Output="+s);
}
}
its equivalent number in the Hexadecimal number system.Hexadecimal Number system is a number
system which can represent a number in any other number system in terms of digits ranging from
0 to 9 and then A-F only.This number system consists of only sixteen basic digits i.e. 0,1,
2,3,4,5,6,7,8,9,A,B,C,D,E and F.Here 10 is represented as A,11 as B and so on till 15 which is
represented as F.For example :47 in hexadecimal number system can be represented as 2F in the
Hexadecimal number system.
import java.io.*;
public class DecimalToHexa
{
public static void main(String args[])throws Exception
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
System.out.print("Enter the number in decimal number system:");
int n=Integer.parseInt(stdin.readLine());
int r;
String s=""; //variable to store the result after
//sorting the digits as in hexadecimal number system
char dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(n>0)
{
r=n%16; //finding the remainder
s=dig[r]+s; //adding the remainder to the result
n=n/16;
}
System.out.print("Output="+s);
}
}