The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Example program convert binary to decimal

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Example program convert binary to decimal Posted: Mar 21, 2016 10:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Example program convert binary to decimal
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

    • 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)

    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8.  public static void main(String[] args) {
    9.         
    10.  System.out.println("\nBinary representation of 1: ");
    11.  System.out.println(Integer.toBinaryString(1));
    12.  System.out.println("\nBinary representation of 4: ");
    13.  System.out.println(Integer.toBinaryString(4));
    14.  System.out.println("Binary representation of 10: ");
    15.  System.out.println(Integer.toBinaryString(10));
    16.  System.out.println("\nBinary representation of 12: ");
    17.  System.out.println(Integer.toBinaryString(12));
    18.  System.out.println("\nBinary representation of 120: ");
    19.  System.out.println(Integer.toBinaryString(120));
    20.  System.out.println("\nBinary representation of 500: ");
    21.  System.out.println(Integer.toBinaryString(500));

    22. }
    23.  
    24. }



    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100
    9. Binary representation of 120: 
    10. 1111000
    11. Binary representation of 500: 
    12. 111110100

    2.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. import java.util.Stack;
    3. public class ConvertDecimalToBinary{
    4.  
    5.     /**
    6.      *www.instanceofjava.com
    7.      */
    8.  
    9.  
    10.  public static void main(String[] args) {
    11.         
    12. Scanner in = new Scanner(System.in);
    13.          
    14. // Create Stack object
    15. Stack<Integer> stack = new Stack<Integer>();
    16.      
    17. //Take  User input from keyboard
    18.  System.out.println("Enter decimal number: ");
    19.  int num = in.nextInt();
    20.     
    21. while (num != 0)
    22. {
    23.    int d = num % 2;
    24.    stack.push(d);
    25.    num /= 2;
    26.  
    27.  } 
    28.      
    29.  System.out.print("\nBinary representation is:");
    30.  
    31. while (!(stack.isEmpty() ))
    32.  {
    33.    System.out.print(stack.pop());
    34.  }
    35.         
    36.  
    37. System.out.println();
    38.  
    39. }
    40.  
    41. }
    42.  
    43. }


    Output:

    1. Enter decimal number: 
    2. 12
    3.  
    4. Binary representation is:1100

    java program convert decimal to binary


    3.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8. public static void convertDeciamlToBinary(int num){
    9.  
    10.   int binary[] = new int[40];
    11.          int index = 0;
    12.  while(num > 0){
    13.            binary[index++] = num%2;
    14.            num = num/2;
    15.  }

    16. for(int i = index-1;i >= 0;i--){
    17.            System.out.print(binary[i]);
    18. }
    19.  
    20. }
    21.  
    22.  public static void main(String[] args) {
    23.         
    24. System.out.println("Binary representation of 1: ");
    25. convertDeciamlToBinary(1);
    26.  
    27. System.out.println("\nBinary representation of 4: ");
    28. convertDeciamlToBinary(4);
    29.  
    30. System.out.println("\nBinary representation of 10: ");
    31. convertDeciamlToBinary(10);
    32.  
    33. System.out.println("\nBinary representation of 12: ");
    34.  convertDeciamlToBinary(12);
    35.  
    36. }
    37.  
    38. }


    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100

    Read: Java Example program convert binary to decimal

    Topic: Implementing an annotation interface Previous Topic   Next Topic Topic: Containers all the way through

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use