Thursday , September 21 2017
Home / Blue J / Java program for binary to decimal conversion

Java program for binary to decimal conversion

This is a java program to convert a binary number to decimal number:
Where the concept is quite simple to do so-
Step 1: extract digits from last   (using number%10) and then making number = number/10;
Step 2: Digit * 2place value of digit
Multiplying the digits from last with the 2 to the power of position of the digit (place value):
Step 3: As seen above adding the each multiplication of Digit * 2place value of digit
Example:  1101 = 2 * 1 + 21 * 0 + 22 * 1 + 23 * 1 = 13.

ALSO CHECK: c program for binary to decimal conversion

import java.util.Scanner;		
class binarytodecimal
{
	public static void main(String args[])
	{
		int bin, dec=0,k=1;
		Scanner scanner=new Scanner(System.in);
		System.out.println("Enter the binary number : ");
		bin=scanner.nextInt();
		while(bin!=0)
		{ 
			dec=dec+ (bin%10)*k;     // extracting the last digit of a binary number and multiplying it with 2  digits place value
			k*=2;
			bin/=10;     // removing the last digit of a binary number 
		}
		System.out.println("Decimal number = "+dec);
	}
}

OUTPUT:

Java program for binary to decimal conversion
Output: Program for binary to decimal

Below is the video tutorial for java program to convert binary to decimal

For any of the doubts related to this program leave the comment below we will help you to get the exact solution of it.

Tags: Java program for binary to decimal conversion.

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

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