Write a Java program to add two numbers without using any arithmetic operators
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner; public class Javaexcercise { public static void main(String[] arg) { int num1, num2 ; Scanner in = new Scanner(System.in); System.out.print("Enter 1st number: "); num1 = in.nextInt(); System.out.print("Enter 2nd number: "); num2 = in.nextInt(); while(num2 != 0){ int carry = num1 & num2; num1 = num1 ^ num2; num2= carry << 1; } System.out.print("Sum: "+num1); System.out.print("\n"); } }
Result:
Enter 1st number: 50
Enter 2nd number: 60
Sum: 110
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Enter 1st number: 50
Enter 2nd number: 60
Sum: 110
need an explanation for this answer? contact us directly to get an explanation for this answer