Q:

Write a Java program to add two numbers without using any arithmetic operators

0

Write a Java program to add two numbers without using any arithmetic operators

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java programming language Basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to add all the digits of a gi... >>
<< Write a Java program to check if a positive number...