-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Saturday, May 9, 2020

Java program to add two binary numbers

  • May 09, 2020
  • by


Java program to add two binary numbers


In this Java Program, we perform addition of two binary numbers.


Binary number  system consists of 0's and 1's hence a binary number is made up of only 0 and 1.


                                             Adding Numbers 11100 & 10101

                                                                       

                                                                       1                               11                                 1

    11100                        11100                    11100                         11100                           11100       

+  10101         👉       +  10101       👉      +  10101          👉       +  10101           👉        +  10101              

------------                    -------------                -------------                    -------------                   -------------

            1                              01                          001                          0001                          110001


Step 1 : Import java.util.Scanner Class


Step 2 : Create two variables b1 and b2 to hold the two binary numbers entered by the user.


Step 3 : Initialize int i = 0 and carry = 0.


Step 4 : Create an array int[] sum to hold the output binary number


Step 5 : By using the Scanner, take the two binary numbers from the user and store it in b1 and b2.


Step 6 : Close the Scanner to avoid the memory leak.


Step 7 : Add the two binary numbers bit by bit storing the result into the array.


Step 8 : Display the result by using System.out.println method.


Program :


import java.util.Scanner;


public class AddTwoBinaryNumbers

    {

        public static void main (String[] args)

        {

            //Two variables to hold two input binary numbers

            long b1, b2;

            int i = 0, carry = 0;

           

            //This is to hold the output binary number

            int[] sum = new int[10];

           

            //To read the input binary numbers entered by the user

            Scanner scanner = new Scanner(System.in);

           

            //getting first binary number from user

            System.out.print("Enter first binary number:");

            b1 = scanner.nextLong();

           

            //getting second binary number from the user

            System.out.print("Enter second binary number:");

            b2 = scanner.nextLong();

           

            //closing scanner after use to avoid memory leak

            scanner.close();

           

            while (b1 != 0 || b2!=0)

               {

                sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);

                carry = (int)((b1 % 10 + b2 % 10 + carry)/2);

                b1 = b1 / 10;

                b2 = b2 / 10;

               

                }

           

            if (carry !=0)

                {

                    sum[i++] = carry;

                }

            --i;

            System.out.print("Output:");

            while (i >=0)

                {

                    System.out.print(sum[i--]);

                }

            System.out.print("\n");

                }

               

            }



Output:


Enter first binary number: 11100

Enter second binary number: 10101

Output: 110001


For the execution of the same do visit my YouTube channel:


https://youtu.be/RNfVTsCfNrU

A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages