# using stacks to convert decimal to binary

Page 1 of 1

## 3 Replies - 10853 Views - Last Post: 06 December 2008 - 10:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=75250&amp;s=ed1b1aafcfae6e2ca81dbe8479ae3030&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 napolean

• New D.I.C Head

Reputation: 1
• Posts: 6
• Joined: 13-November 08

# using stacks to convert decimal to binary

Posted 05 December 2008 - 04:43 PM

so i need the user to input a decimal number and have it output in binary. my teacher says we have to use stacks to do it. she also said we cant use recursion to solve the problem. my code so far takes in the user input but will not convert.

```import java.util.*;

public class convert2
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number you would like to convert ");	//user input deciaml #
int n = keyboard.nextInt();
Stack<Integer> stack = new Stack<Integer>(); //declare stack of integers
do
{
int q = n/2;
int r = n%2;
stack.push(r);
n = q;
}
while(n!=0);

System.out.println("The number in binary is: " + n);

while(!stack.empty())
{
stack.pop();
System.out.println(n);
}
}
}

```

here is my output

Enter the number you would like to convert
42
The number in binary is: 0
0
0
0
0
0
0

Is This A Good Question/Topic? 0

## Replies To: using stacks to convert decimal to binary

### #2 ibaraku

Reputation: 3
• Posts: 190
• Joined: 12-May 07

## Re: using stacks to convert decimal to binary

Posted 05 December 2008 - 05:12 PM

Disregard my last post, I just tested and it doesn't quite work, sorry

This post has been edited by ibaraku: 05 December 2008 - 05:43 PM

### #3 ibaraku

Reputation: 3
• Posts: 190
• Joined: 12-May 07

## Re: using stacks to convert decimal to binary

Posted 06 December 2008 - 02:05 AM

Ok sorry for the last post, I tested this and it looks like it works, you might want to add some code to make it look better...

```

import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number you would like to convert ");	//user input deciaml #
int n = keyboard.nextInt();
Stack<Integer> stack = new Stack<Integer>(); //declare stack of integers

if(n == 0) {
System.out.println("BINARY is 0000");
}
else if(n == 1) {
System.out.println("Binary is: 0001");
}
else {
while(n != 0) {
int q = n/2;
int r = n%2;
stack.push®;
n = q;
}
}

while(!stack.empty()) {
System.out.print(stack.pop());
}//end while

System.out.println(" ");
}
}

```

### #4 napolean

• New D.I.C Head

Reputation: 1
• Posts: 6
• Joined: 13-November 08

## Re: using stacks to convert decimal to binary

Posted 06 December 2008 - 10:51 AM

that is just what i needed i have made it loop so it works great now...
thank you thank you thank you

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }