Write a SCHEME procedure called (add-binary x y) that adds two binary numbers re
ID: 3709453 • Letter: W
Question
Write a SCHEME procedure called (add-binary x y) that adds two binary numbers represented as a list of 0’s & 1’s. For example: (add-binary (1 0 0) (1 0)) ? (1 1 0)
I was able to write the function to to add the two numbers together:
(define add-binary
(lambda (x y)
(cond
((null? x) y)
((null? y) x)
(else (cons (+ (car x) (car y)) (add-binary (cdr x) (cdr y)))))))
Now I just need help with the helper function to deal with the carry for binary addition and it needs to have one more parameter for the carry:
(define binary-carry
(lambda (x y carry)
Any help is much appreciated!!
Explanation / Answer
import java.util.Scanner;
class BinaryAddition {
public static void main(String args[]){
Scanner input = new Scanner( System.in );
System.out.print("Enter first number: ");
String firstNo =input.nextLine();
System.out.print("Enter second number: ");
String secondNo =input.nextLine();
int result = Integer.parseInt(firstNo,2) + Integer.parseInt(secondNo,2);
System.out.println("Addition result: " + Integer.toBinaryString(result));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.