Java Program to Convert a Decimal to Binary Number using Stacks

This is a Java Program to find the binary equivalent of a decimal number using stacks. Stack is an area of memory that holds all local variables and parameters used by any function and remembers the order in which functions are called so that function returns occur correctly. ‘push’ operation is used to add an element to stack and ‘pop’ operation is used to remove an element from stack. ‘peek’ operation is also implemented returning the value of the top element without removing it. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure.

For converting decimal number to binary, initially we push all the binary digits formed into the stack. After the entire number has been converted into the binary form, we pop one digit at a time from the stack and print it. Therefore we get the decimal number converted into its proper binary form.

Here is the source code of the Java program to convert decimal number to binary using stacks. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to Convert a Decimal Number 
  3.  *  to Binary Number using Stacks
  4.  */
  5.  
  6. import java.util.*;
  7.  
  8. /*  DecimalToBinaryUsingStacks  */
  9. public class DecimalToBinaryUsingStacks
  10. {
  11.     public static void main(String[] args) 
  12.     {    
  13.         Scanner scan = new Scanner(System.in);
  14.         /* Creating Stack object  */
  15.         Stack<Integer> stk = new Stack<Integer>();
  16.         /* Accepting number */        
  17.         System.out.println("Enter decimal number");
  18.         int num = scan.nextInt();
  19.  
  20.         while (num != 0)
  21.         {
  22.             int d = num % 2;
  23.             stk.push(d);
  24.             num /= 2;
  25.         }        
  26.         /* Print Binary equivalent */
  27.         System.out.print("\nBinary equivalent = ");
  28.         while (!(stk.isEmpty() ))
  29.         {
  30.             System.out.print(stk.pop());
  31.         }
  32.         System.out.println();
  33.     }
  34. }

Enter decimal number
12345
 
Binary equivalent = 11000000111001
 
 
Enter decimal number
99
 
Binary equivalent = 1100011
 
 
Enter decimal number
24162
 
Binary equivalent = 101111001100010
 
 
Enter decimal number
347562318
 
Binary equivalent = 10100101101110110000101001110

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.