• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
September 01, 2022 |1.1K Views

Java Program to Convert Integer Values into Binary

  Share  2 Likes
Description
Discussion

In this video, we will write a Java Program to Convert Integer Values into Binary.

Examples:

Input: = 45
Output: = 101101

Here we see three methods to convert integer values into binary:

1. Using Array: Here we convert an integer to binary and divide the number by 2 until it becomes 0. In each step take the into modulo by 2 and then store the remainder into an array. At last, print it in reverse order.

2. Using Stack: As we know that the binary number consists of 0 and 1. Here we convert an integer to binary and divide the number by 2 until it becomes 0. In each step take the into modulo by 2 and then store the remainder in a stack. Now we store the remainder into a stack then simply pop one by one element and print it.

3. Using toBinary String(): This is an inbuilt method of the Integer class of Java. Here the time complexity is O(log2N) and auxiliary space is O(log2N).

Java Program to Convert Integer Values into Binary:
https://www.geeksforgeeks.org/java-program-to-convert-integer-values-into-binary/