Java Program For Binary Addition - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we are going to write a Java program for Binary Addition. As part of the implementation of Binary Addition, we will also check whether a number is binary or not.

package com.onlinetutorialspoint.patterns;

import java.util.Scanner;

public class Addition_Binary_Numbers {

    public static void main(String args[]) {
        long binary1, binary2;
        int i = 0, remainder = 0;
        int[] sum = new int[30];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter any binary number : ");
        binary1 = sc.nextLong();
        System.out.print("Enter another binary number : ");
        binary2 = sc.nextLong();
        if (isBinary(binary1) && isBinary(binary2)) {
            while (binary1 != 0 || binary2 != 0) {
                sum[i++] = (int) ((binary1 % 10 + binary2 % 10 + remainder) % 2);
                remainder = (int) ((binary1 % 10 + binary2 % 10 + remainder) / 2);
                binary1 = binary1 / 10;
                binary2 = binary2 / 10;
            }
            if (remainder != 0) {
                sum[i++] = remainder;
            }
            --i;
            System.out.print("Sum of two binary numbers : ");
            while (i >= 0) {
                System.out.print(sum[i--]);
            }
        } else {
            System.out.println("Please enter valid binary number..");
        }
        
    }

    public static boolean isBinary(long number) {
        while (number != 0) {
            if (number % 10 > 1) {
                return false;
            }
            number = number / 10;
        }
        return true;
    }

}

Output :
javac Addition_Binary_Numbers.java
>java Addition_Binary_Numbers

Enter any binary number : 1011011
Enter another binary number : 1110111
Sum of two binary numbers : 11010010
Happy Learning 🙂

Decimal To Octal Conversion Java Program
Java Program To find A Number is ArmStrong or Not ?
Java program for Triangle Part – 4
Java program for Triangle Part – 3
Octal To Decimal Conversion Java Program
Java Program to Find the GCD of Two Numbers
Java Program for Check Octal Number
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
C Program – Addition of two Matrices Example
C Program Addition and Subtraction without using + – Operators
Java Program for String Sorting Example
Java Program to Print Pattern Triangle
Java Program to Print Diamond Pattern

Decimal To Octal Conversion Java Program
Java Program To find A Number is ArmStrong or Not ?
Java program for Triangle Part – 4
Java program for Triangle Part – 3
Octal To Decimal Conversion Java Program
Java Program to Find the GCD of Two Numbers
Java Program for Check Octal Number
Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Decimal To Binary Conversion Java Program
C Program – Addition of two Matrices Example
C Program Addition and Subtraction without using + – Operators
Java Program for String Sorting Example
Java Program to Print Pattern Triangle
Java Program to Print Diamond Pattern






Rostislav Kaleta
May 28, 2018 at 1:27 pm - Reply 

“here is the code you want” No how it works, no why, when its good to choose this, its downsides.No explanation why sum integer array has predefined 30 spaces.










Rostislav Kaleta
May 28, 2018 at 1:27 pm - Reply 

“here is the code you want” No how it works, no why, when its good to choose this, its downsides.No explanation why sum integer array has predefined 30 spaces.




“here is the code you want” No how it works, no why, when its good to choose this, its downsides.No explanation why sum integer array has predefined 30 spaces.