Java Program to Convert Binary to Decimal

Problem Description

Write a Java program that takes a binary number as input and convert it to a decimal number.

Binary Number System: The binary number system also known as base 2 number system was invented in 1689 by Gottfried Leibniz it contains two symbol 0 and 1. The base 2 number system is a positional notation with radix of 2. Each of the digit is called as Bit.

Decimal Number System: The decimal number system is one of the most popular number system used in day to day life. It is also known as base 10 number system as it uses digits from 0 to 9
that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In order to represent number system, we need a base. Decimal numbers are represented in the base 10. Base 10 is the most common base in the world.

Program/Source Code

Here is the source code of the Java Program to Convert the Given Binary Number into its Equivalent Decimal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Convert Binary to Decimal
 */
 
import java.util.Scanner;
class Binary_Decimal
 {
     Scanner scan;
     int num;
     void getVal() 
      {
                  System.out.println("Binary to Decimal");
   	 scan = new Scanner(System.in);
	 System.out.println("\nEnter the number :");
	 num = Integer.parseInt(scan.nextLine(), 2);
      }
    void convert() 
      {
                  String decimal = Integer.toString(num);
   	System.out.println("Decimal Value is : " + decimal);
      }
}
class  MainClass 
{
    public static void main(String args[]) 
      {
          Binary_Decimal obj = new Binary_Decimal();
           obj.getVal();
           obj.convert();
      }
}
Program Explanation

1. The program begins by importing the Scanner class from the java.util package. This class is used to read input from the user.
2. The Binary_Decimal class contains two instance variables: scan and num. The scan variable is a reference to a Scanner object, while the num variable is used to store the binary number that the user enters.
3. The getVal() method prompts the user to enter a binary number and then stores the input in the num variable. The Integer.parseInt() method is used to convert the user’s input from a string to an integer in base 2 (binary).
4. The convert() method simply prints the decimal equivalent of the binary number stored in the num variable.
5. The MainClass class contains the main() method, which is the entry point for the program. The main() method creates an instance of the Binary_Decimal class and then calls the getVal() and convert() methods to convert a binary number to decimal.

advertisement
advertisement

Time complexity: O(n)
The time complexity of the binary to decimal conversion is O(n), where n is the number of binary bits.

Space Complexity: O(1)
The Space Complexity of the above program is O(1) as no additional extra space is used.

Program Output

In this case, we are entering the binary number “1010” as input.

$ javac MainClass.java
$ java MainClass
 
Binary to Decimal
 
Enter the number :
1010
Decimal Value is : 10

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.