forked from Manish1Gupta/Coding-Community-Contributions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec2Hex.java
More file actions
34 lines (31 loc) · 659 Bytes
/
Dec2Hex.java
File metadata and controls
34 lines (31 loc) · 659 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* java code to convert decimal numbers into hexa-decimal numbers */
import java.util.Scanner;
class Dec2Hex
{
public static void main(String[] args)
{
Scanner INP = new Scanner(System.in);
System.out.print("Enter decimal number : ");
int dec = INP.nextInt();
int temp = dec;
int ascii = 0;
int rem = 0;
String hex = "";
while(temp > 0)
{
rem = temp % 16;
temp = temp / 16;
if(rem < 10)
{
ascii = (rem + 48);
hex = (char)ascii + hex;
continue;
}
ascii = (rem + 55);
hex = (char)ascii + hex;
}
System.out.println("The equivalent hexadecimal number is : " + hex);
INP.close();
return;
}
}