Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Last Updated : 11 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the array in reverse order. Java // Java Program to Convert a Decimal Number // to Binary Number using Arrays as Stacks import java.util.*; public class DecimalToBinary { static int arr[] = new int[1000]; // maintaining count variable // as the top of the stack static int count; // push at the count index and increment the count public static void push(int n) { arr[count++] = n; } // pop all the elements starting // from count-1 till 0 public static void pop() { for (int i = count - 1; i >= 0; i--) { System.out.print(arr[i]); } } public static void main(String args[]) { int num = 46; while (num > 0) { int r = num % 2; push(r); num /= 2; } System.out.print("Binary equivalent: "); pop(); } } OutputBinary equivalent: 101110 Time complexity: O(logn) for given input number n Comment More infoAdvertise with us Next Article Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks zack_aayush Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek 3 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15. 6 min read Program to convert Primitive Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case 3 min read Java Program to Convert Hex String to Byte Array Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadecimal string. Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte 4 min read Java Program to Convert Byte Array to Hex String Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadec 5 min read Java Program to Convert Byte Array to Long A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Given an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo 3 min read Java Program to Swap Two Numbers Using Bitwise XOR Operation Given two numbers x and y. We have to write a Java Program to Swap the contents of two numbers using Bitwise XOR Operation. Input 1: x = 5, y = 10 Output : x = 10, y = 5 Explanation : 1. x = x ^ y -> x = 15 2. y = x ^ y -> y = 5 3. x = x ^ y -> x = 10 Input 2: x = 15, y = 20 Output : x = 20 2 min read Java Program to Add Two numbers Without using Arithmetic Operator Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, â, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply prin 2 min read Like