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 Must Do Coding Questions - Topic-wise Z zack_aayush Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like