The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Example Program to 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 to convert binary to decimal Posted: Mar 21, 2016 8:14 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 to 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

  • Famous interview java question on conversions . 
  • We can convert binary to decimal in two ways
    1.Using Integer.parseInt() method
    2.Using custom logic



1.Write a Java Program to convert binary to decimal number in java without using Integer.parseInt() method.

  1. import java.util.Scanner;
  2. public class DecimalFromBinary {
  3.  
  4.     /**
  5.      *www.instanceofjava.com
  6.      */
  7.  
  8.  public static void main(String[] args) {
  9.         
  10.  Scanner in = new Scanner( System.in );
  11.  System.out.println("Enter a binary number: ");
  12.  
  13.   int  binarynum =in.nextInt();
  14.   int binary=binarynum;
  15.         
  16. int decimal = 0;
  17. int power = 0;
  18.  
  19. while(true){
  20.  
  21.  if(binary == 0){
  22.  
  23.         break;
  24.  
  25.  } else {
  26.  
  27.    int tmp = binary%10;
  28.    decimal += tmp*Math.pow(2, power);
  29.    binary = binary/10;
  30.    power++;
  31.  
  32.  }
  33. }
  34.         System.out.println("Binary="+binary+" Decimal="+decimal); ;
  35. }
  36.  
  37. }

Output:


  1. Enter a binary number: 
  2. 101
  3. Binary=101 Decimal=5


2.Write a Java Program to convert binary to decimal number in java using Integer.parseInt() method.



  1. import java.util.Scanner;
  2. public class ConvertBinaryToDecimal{
  3.  
  4.     /**
  5.      *www.instanceofjava.com
  6.      */
  7.  
  8.  public static void main(String[] args) {
  9.         
  10.  Scanner in = new Scanner( System.in );
  11.  
  12.  System.out.println("Enter a binary number: ");
  13.  String binaryString =input.nextLine();
  14.  
  15.  System.out.println("Result: "+Integer.parseInt(binaryString,2));

  16.  
  17. }
  18.  
  19. }


Output:


  1. Enter a binary number:
  2. 001
  3. Result: 1

java program to convert binary to decimal

Read: Java Example Program to convert binary to decimal

Topic: Containers all the way through Previous Topic   Next Topic Topic: Kids : Vehicles - Playlist

Sponsored Links



Google
  Web Artima.com   

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