Showing posts with label Java Program to Convert Binary to Decimal Using Integer.parseInt() Method. Show all posts
Showing posts with label Java Program to Convert Binary to Decimal Using Integer.parseInt() Method. Show all posts
on Leave a Comment

Java Program to Convert Binary to Decimal

This java program converts binary number into decimal number. First, you have to enter a binary number and then convert binary number to decimal number and finally print decimal equivalent of that binary number.

Java Program to Convert Binary Number to Decimal Number

import java.util.Scanner;

public class BinaryToDecimal
{
    public static void main(String args[])
    {
        int binary, decimal=0, i=1;
        Scanner read = new Scanner(System.in);
  
        System.out.print("Enter any Binary Number : ");
        binary = read.nextInt();
        
        int bin = binary;   
        int remainder = 1;

        while(binary > 0)
        {
            remainder = binary%10;
            decimal = decimal + remainder*i;
            i = i*2;
            binary = binary/10;
        }
  
        System.out.print("Decimal Equivalent of " +bin+ " is : "+decimal);
    }
}

Output:

Enter any Binary Number : 1111
Decimal Equivalent of 1111 is : 15

Java Program to Convert Binary to Decimal Using Integer.parseInt() Method

import java.util.Scanner;
class BinaryToDecimal {
    public static void main(String args[]){
       Scanner input = new Scanner( System.in );
       System.out.print("Enter any Binary Number: ");
       String binary =input.nextLine();

       System.out.println("Decimal Equivalent of " +binary+ " is : "+Integer.parseInt(binary,2));
    }
}

Output:

Enter any Binary Number: 1101
Decimal Equivalent of 1101 is : 13