Tuesday, September 8, 2020

JAVA Program | Binary Number Addition | Diploma Computer Engineering | MCA | BCA | Information Technology

 

//JAVA Program to performed addition of Binary Number.

package Diploma;

import java.util.Scanner;

public class Binaryadd

{

 public static void main(String[] args)

 {

  long binary1, binary2;

  int i = 0, remainder = 0;

  int[] sum = new int[20];

  Scanner in = new Scanner(System.in);


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

  binary1 = in.nextLong();

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

  binary2 = in.nextLong();


  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--]);

  }

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

 }

}

/*

Output

Input first binary number: 101

Input second binary number: 111

Sum of two binary numbers: 1100

*/







No comments:

Post a Comment

CMS - 2024 || JAC INTERMEDIATE EXAMINATION - 2024 || 12th COMPUTER SCIENCE!

  Part-A Multiple Choice Questions 1) Who is the developer C++? a) Von Neumann b) Dennis M. Ritchie c) Charles Babbage d) Bjarne Stroustrup ...