Decimal to hexadecimal in JAVA programming


import java.util.Scanner;

public class DecimalToHexa {

    public static void main(String[] args) {
        Scanner myScanner=new Scanner(System.in);
       
        int n;
       
        int[] arr=new int[100];
        char ch[]=new char[100];
        System.out.print("Enter a decimal number : ");
        n=myScanner.nextInt();
        int i=0,rem;
        while (n!=0) {
            rem=n%16;
            if(rem<10){
                rem=rem+48;
            }
            else{
                rem=rem+55;
            }
            ch[i++]=(char) rem;
            n=n/16;
        }
        System.out.print("Hexadecimal value is : ");
        for (int j = i-1; j>=0; j--) {
           
            System.out.print(ch[j]);
        }
        System.out.println();
    }

}
//Enter a decimal number : 45678
//Hexadecimal value is : B26E

Share this

Related Posts

Previous
Next Post »