-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathDecimalToHexadecimal.java
44 lines (27 loc) · 1.19 KB
/
DecimalToHexadecimal.java
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
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class DecimalToHexaDecimal
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking input from the user
System.out.println("Enter The Decimal Number : ");
int inputNumber = sc.nextInt();
//Copying inputNumber into copyOfInputNumber
int copyOfInputNumber = inputNumber;
//Initializing hexa to empty string
String hexa = "";
//Digits in HexaDecimal Number System
char hexaDecimals[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//Defining rem to store remainder
int rem = 0;
//See the below explanation to know how this loop works
while (inputNumber > 0)
{
rem = inputNumber%16;
hexa = hexaDecimals[rem] + hexa;
inputNumber = inputNumber/16;
}
System.out.println("HexaDecimal Equivalent of "+copyOfInputNumber+" is "+hexa);
}
}