Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class Program {
- public static void decToBinString(int number) {
- String bin = "";
- while (number != 0) {
- bin += number % 2;
- number /= 2;
- }
- // reverse
- for(int i = bin.length() - 1; i >= 0; i--) {
- System.out.print(bin.charAt(i));
- }
- }
- public static void decToBinStack(int number) {
- Stack<Integer> bin = new Stack<Integer>();
- while (number != 0) {
- bin.push(number % 2);
- number /= 2;
- }
- while (!bin.empty()) {
- System.out.print(bin.pop());
- }
- }
- public static void main(String[] args) {
- decToBinString(25);
- System.out.println();
- decToBinStack(25);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement