• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » Convert Binary To Decimal

Convert Binary To Decimal

Last Updated on: August 13, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Convert Binary Number to Decimal Number
  • Binary To Decimal How To using Integer.parseInt() Method
  • Binary To Decimal How To Using Custom Method

Convert Binary To Decimal How To: In this blog post, we will discuss how to convert a binary number system into a decimal number system. Binary numbers are the core of each and every computational device. They are represented by the base2 number system and use only two digits: 0s and 1s.

For a Better understanding of these programs, you must have some programming knowledge on Java’s few topics like:

  • Java Methods
  • Java Operators
  • Java while Loop
Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Convert Binary Number to Decimal Number

To convert a binary to decimal how to, start from the right side of the number and multiply each digit by its place value. The place values in binary go up in powers of 2, starting at 0. Add all the products together to get the equivalent decimal number.

We can convert Binary To Decimal in the following 2 ways that are:

  • Using Integer.parseInt() method of Integer class.
  • By using the custom method

Binary To Decimal How To using Integer.parseInt() Method

The Integer class is a wrapper class in Java that has a parseInt() method which can be used to convert a String into an int. In the program below, we’re using the parseInt() method with two arguments: a String and an int. The first argument is parsed as a signed decimal integer according to the value of the second argument (the radix).

We are converting a binary number to its decimal equivalent using the Integer.parseInt() method. We are passing in the binary number as a string with a radix value of 2, as binary numbers follow the base2 numbering system.

Note: The characters in the passed String should only be in the digits form.

package com.softwaretestingo.basic;
import java.util.Scanner;
public class BinarytoDecimalEx1 
{
	public static void main(String[] args) 
	{
	Scanner scn = new Scanner(System.in);
        System.out.println("Enter a Binary Number : ");
	
        // Enter a binary number in string form
        String binaryNumber = scn.nextLine();

        // passing binary number and base in Integer.parseInt() method
        // converting binary to decimal, return type int
        int decimalNumber = Integer.parseInt(binaryNumber, 2);		

        // printing the values of binary and decimal equivalent
        System.out.println("Binary Number : " + binaryNumber);
        System.out.println("Decimal Number Equivalent : " + decimalNumber);
	}
}

Output:

Enter a Binary Number :  10010
Binary Number : 10010
Decimal Number Equivalent : 18

Binary To Decimal How To Using Custom Method

We can convert a binary number to its decimal equivalent by extracting each binary digit from the right side of the number, dividing it by 10 until the binary number becomes 0. For each extracted digit, we multiply it by consecutive powers of 2 (starting from 0) and add all product values together to get an equivalent decimal number of the respective binary number.

package com.softwaretestingo.basic;
import java.util.Scanner;
public class BinarytoDecimalEx2 
{
	public static void main(String[] args) 
	{
		Scanner scn = new Scanner(System.in);

		System.out.println("Enter a Binary Number : ");
		// input a binary number
		long binaryNumber = scn.nextLong();

		// calling the method BinaryToDecimal and storing it in decimalNumber
		long decimalNumber = BinaryToDecimal(binaryNumber);

		// printing the values of binary and decimal equivalent
		System.out.println("Binary Number : " + binaryNumber);
		System.out.println("Decimal Number Equivalent : " + decimalNumber);

	}
	public static long BinaryToDecimal(long binaryNum)
	{
		long decimalEquivalent = 0;
		long lastDigit = 0;
		int power = 0;

		// binary to decimal conversion
		while(binaryNum > 0)
		{
			// getting the right most digit after every division by 10
			lastDigit = binaryNum % 10;
			// adding the product of lastDigit and  2^power 
			decimalEquivalent += lastDigit * Math.pow(2, power);
			power++;
			binaryNum /= 10;
		}

		return decimalEquivalent;
	}
}

Output:

Enter a Binary Number :  10010
Binary Number : 10010
Decimal Number Equivalent : 18

Conclusion:

Finally, we have reached the end of the Convert Binary To Decimal Java Program. You can try your own and if you found any difficulty then let us know in the comment section and we will try to help you with that.

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers