3 Replies - 7682 Views - Last Post: 06 December 2008 - 10:51 AM Rate Topic: -----

#1 napolean  Icon User is offline

  • New D.I.C Head

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

using stacks to convert decimal to binary

Post icon  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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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(" ");
	}
}



Was This Post Helpful? 1
  • +
  • -

#4 napolean  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1