### Convert Decimal to Hexadecimal using stack in java

You all are aware of Stacks. It performs two basic operations push and pop. The push operation adds an element to the top of the list, or initializing the stack if it is empty and the pop operation removes an item from the top of the list. Using these operations, we have converted a decimal number into Hexadecimal .
• Divide the decimal number by 16
•  Treat the division as an integer division. Write down the remainder (in hexadecimal).
• Divide the result again by 16.
•  Treat the division as an integer division. Repeat step 2 and 3 until result is 0.
• The hex value is the digit sequence of the remainders from the last to first.
```HEXADECIMAL  0 1 2 3 4 5 6 7 8 9 A  B  C  D  E  F
DECIMAL      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
```

Java version of decimal to hexadecimal is given bellow :
```/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stack;

import java.util.Scanner;

/**
*
* @author ACHCHUTHAN
*/
public class decimal2hexa extends Stack {

String digits = "0123456789ABCDEF";
int decimal;
int size;

public decimal2hexa(int n) {
super(n);
System.out.println("Enter nonnegative integer :");
Scanner input = new Scanner(System.in);
decimal = input.nextInt();
size = 0;
}

/**
* push the value to stack
*/
public void setHex2Stack() {
if (decimal == 0) {
push(0);
}
while (decimal > 0) {
int digit = decimal % 16;
push(digits.charAt(digit));
decimal = decimal / 16;
size++;
}
}

/**
* @return hexadecimal string value
*/
public String getHexa() {
String h = "";
while (!isEmpty()) {
h = h + pop();
}
return h;
}

public static void main(String args[]) {
decimal2hexa d = new decimal2hexa(16);
d.setHex2Stack();
d.print();
System.out.println("Hexadecimal : " + d.getHexa());
decimal2hexa d2 = new decimal2hexa(16);
d2.setHex2Stack();
d2.print();
System.out.println("Hexadecimal : " + d2.getHexa());

}
}
```
Output of this program :

run:
Enter nonnegative integer :
31
Contents of Stack :
F | 1 |
Enter nonnegative integer :
256
Contents of Stack :
0 | 0 | 1 |