Understanding the while loop in in this program Binary addition in java -
i found solution binary addition in java , struggling understand program, not program found on internet. while loop im struggling understand. thanks
package java_excersises; import java.util.scanner; public class practice17 { public static void main(string args[]) { long binary1, binary2; int = 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 2 binary numbers: "); while (i >= 0) { system.out.print(sum[i--]); } system.out.print("\n"); } }
Comments
Post a Comment