Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 25 December 2017

Java program to convert decimal to Hexadecimal

import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;
public class convert
 {
 static char hexaChar[] = {'F','E','D','C','B','A'}; 
static String findHex(int no)
String result =""; 
int rem;
while(no>0)
{
 rem = no % 16; 
if(rem > 9)
result = hexaChar[15 - rem] + result; 
}
else
result = rem+result; 
no = no/16; 
return result;
}

 public static void main(String[] args)
 { 
Scanner sc = new Scanner(System.in); System.out.println("Enter a number "); int num = sc.nextInt(); System.out.println("Hexa Decimal : "+findHex(num)); 
}
}

No comments:

Post a Comment