Page 491 - Cs_withBlue_J_C11_Flipbook
P. 491

22              while (bin1 != 0 || bin2 != 0)
                   23              {

                   24                  add[i++] = (int)((bin1 % 10 + bin2 % 10 + carry) % 2);
                   25                  carry = (int)((bin1 % 10 + bin2 % 10 + carry) / 2);

                   26                  bin1 = bin1 / 10;
                   27                  bin2 = bin2 / 10;

                   28              }
                   29              if (carry != 0)

                   30              {
                   31                  add[i++] = carry;

                   32              }
                   33              --i;
                   34              System.out.print("Output: ");

                   35              for(j=i;j >= 0;j--)

                   36              {
                   37                  System.out.print(add[j]);
                   38              }

                   39              System.out.println("\n-------------------------");
                   40          }

                   41      }

                 The output of the preceding program is as follows:
                 Sum of the Binary Number
                 -------------------------
                 Enter the first binary number : 1100111
                 Enter the second binary number : 11001
                 -------------------------
                 Output: 10000000
                 -------------------------

                                                            Variable Description
                  VARIABLE    DATATYPE                                     DESCRIPTION
                   NAME
                  bin1       long         First binary number

                  bin2       long         Second binary number
                  i          int          counter
                  carry      long         Carry of the sum

                  j          int          Loop variable
                  add[]      int[]        Array to input number



                                                                                                                       489
                                                                                                   Internal Assessment  489
   486   487   488   489   490   491   492   493   494   495   496