Tuesday, November 2, 2010

To print the hexadecimal equivalent of a decimal number in java

//To print the hexadecimal equivalent of a decimal number
import java.io.*;
public class hex_equi
{
public static void main()throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter any no");
int x=Integer.parseInt(br.readLine());
int p=0,b=0,j;
int ar[]=new int[100];
while(x>0)
{
b=x%16;
ar[p++]=b;
x=x/16;
}
for(j=p-1;j>=0;j--)
{
if(ar[j]>9)
System.out.print((char)(ar[j]+55));
else
System.out.print(ar[j]);
}
}
}












No comments: