- We can convert binary to decimal in three ways
1.Using Integer.toBinaryString(int num);
2.Using Stack
3.Using Custom logic
1.Write a Java Program to convert decimal to binary in java using Integer.toBinaryString(int num)
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- System.out.println("\nBinary representation of 1: ");
- System.out.println(Integer.toBinaryString(1));
- System.out.println("\nBinary representation of 4: ");
- System.out.println(Integer.toBinaryString(4));
- System.out.println("Binary representation of 10: ");
- System.out.println(Integer.toBinaryString(10));
- System.out.println("\nBinary representation of 12: ");
- System.out.println(Integer.toBinaryString(12));
- System.out.println("\nBinary representation of 120: ");
- System.out.println(Integer.toBinaryString(120));
- System.out.println("\nBinary representation of 500: ");
- System.out.println(Integer.toBinaryString(500));
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
- Binary representation of 120:
- 1111000
- Binary representation of 500:
- 111110100
2.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- import java.util.Stack;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Create Stack object
- Stack<Integer> stack = new Stack<Integer>();
- //Take User input from keyboard
- System.out.println("Enter decimal number: ");
- int num = in.nextInt();
- while (num != 0)
- {
- int d = num % 2;
- stack.push(d);
- num /= 2;
- }
- System.out.print("\nBinary representation is:");
- while (!(stack.isEmpty() ))
- {
- System.out.print(stack.pop());
- }
- System.out.println();
- }
- }
- }
Output:
- Enter decimal number:
- 12
- Binary representation is:1100
3.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void convertDeciamlToBinary(int num){
- int binary[] = new int[40];
- int index = 0;
- while(num > 0){
- binary[index++] = num%2;
- num = num/2;
- }
- for(int i = index-1;i >= 0;i--){
- System.out.print(binary[i]);
- }
- }
- public static void main(String[] args) {
- System.out.println("Binary representation of 1: ");
- convertDeciamlToBinary(1);
- System.out.println("\nBinary representation of 4: ");
- convertDeciamlToBinary(4);
- System.out.println("\nBinary representation of 10: ");
- convertDeciamlToBinary(10);
- System.out.println("\nBinary representation of 12: ");
- convertDeciamlToBinary(12);
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
You Might Like:
Java Example Program to convert binary to decimal
Java programming interview questions
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
No comments