help: java program to convert decimal number to hexadecimal number - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help: java program to convert decimal number to hexadecimal number

GlenniGlenni Posts: 3Member
Hello all, I have been trying to write a program to convert from decimal to hexadecimal. My program works well except that for inputs =and < 16 the output is always 1. Also there is no output for decimal numbers 32, 48, 64 etc which correspond to 20, 30, 40 etc. I will gladly welcome any help.

import java.util.Scanner;
public class gamuanew{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the number");
int number=input.nextInt();
String output="1";
int n=0;
while (number>=16*n)
{ n++;

}

n=n-1;
int newnum=number-16*n;

if(newnum>=0&&newnum<=9)
{
System.out.print(n+" "+newnum);
}
else if(newnum==number)java
{
while(Math.pow(16,n)<=newnum)
{ n++;
}
n=n-1;
for( int i=0;i<n;i++)
output+="0";
System.out.print(output);
}

else if(newnum>9)
{
switch(newnum)
{
case 10: System.out.print(n+"A"); break;
case 11: System.out.print(n+"B"); break;
case 12: System.out.print(n+"C"); break;
case 13: System.out.print(n+"D"); break;
case 14: System.out.print(n+"E"); break;
case 15: System.out.print(n+"F"); break;
case 16: System.out.print((n+1)+"0"); break;
}
}

}
}

Sign In or Register to comment.